对于JIT和AoT编译器之间的不一致,我一直有一些问题。最近使我困惑的错误是Error: Can't resolve all parameters for IndexedDBCache。IndexedDBCache是一个依赖于string参数的服务:
请注意,当我删除“受保护”属性时,也会出现此问题!
// indexeddb-cache.ts
import { Injectable } from '@angular/core';
@Injectable()
export class IndexedDBCache {
constructor(protected databaseName : string) {}
}我正在使用一家工厂提供服务的版本:
// test-api.cache.factory.ts
import { IndexedDBCache } from '../indexeddb-cache/indexeddb-cache';
export function testIndexedDBCacheFactory() { return new IndexedDBCache('test'); }
// test-api.cache.ts
import { InjectionToken, Provider } from '@angular/core';
import { IndexedDBCache } from '../indexeddb-cache/indexeddb-cache';
import { testIndexedDBCacheFactory } from './test-api.cache.factory';
export const testIndexedDBCache = new InjectionToken<IndexedDBCache>('testIndexedDBCache');
export let testIndexedDBCacheProvider : Provider = {
provide: testIndexedDBCache,
useFactory: testIndexedDBCacheFactory
};注意:这些文件必须按照https://github.com/rangle/angular-2-aot-sandbox#func-in-providers-usefactory-top和https://github.com/rangle/angular-2-aot-sandbox#arrow-function-exports-top进行拆分--不要问我为什么=/
现在,AoT编译器根本不喜欢这个string参数。我已经研究过这个问题,但是只能找到对OpaqueToken的引用(现在被InjectionToken<string>取代了)。我的代码现在改为:
// test-api.cache.factory.ts
import { InjectionToken } from '@angular/core';
import { IndexedDBCache } from '../indexeddb-cache/indexeddb-cache';
const testIndexedDBCacheFactoryToken = new InjectionToken<string>('test');
export function testIndexedDBCacheFactory() { return new IndexedDBCache(testIndexedDBCacheFactoryToken); }显然,这不是编译,因为构造函数只允许使用string参数。我对InjectionTokens或手头的AoT问题没有足够的了解来解决这个问题--有谁能为一个可行的构造提出建议?
关于我的代码和问题的更多上下文可以在角/角#17295上找到。
我尝试过的事情:
protected访问修饰符>完全相同的错误仍然存在InjectionToken<string> > InjectionToken替换工厂中的字符串参数不是合适的参数发布于 2017-06-12 09:09:16
人们对眼前的问题产生了误解。要考虑的类本身不是服务,因此不需要@Injectable属性。更多细节可在创建AoT兼容的服务工厂上找到
https://stackoverflow.com/questions/44432641
复制相似问题