我不确定这是一个错误还是我做错了什么(问题是后者)。 我正在尝试手动实例化名为TodoService的组件。
前两个步骤的一切都很棒
var myProvider = new Provider(TodosService, {
useFactory: (todoAction, http, todoStatsModel, appStore) => {
return new todoAction(TodoAction, http, todoStatsModel, appStore)
},
deps: [TodoAction, Http, TodoStatsModel, AppStore]
});
var injector = Injector.resolveAndCreate([myProvider]);
但是当我试图获得它时会爆炸,所以我可以使用它
var myTodoService = injector.get(TodosService)
我做错了什么(可能在获得)?
答案 0 :(得分:2)
var injector = Injector.resolveAndCreate([myProvider, TodoAction, Http, TodoStatsModel, AppStore]);
我认为你最好不要使用只用
的工厂var injector = Injector.resolveAndCreate(
[TodoService, TodoAction, Http, TodoStatsModel,
provide(AppStore, {useValue: new AppStore('')}
// or
// provide(AppStore, {useFactory: () => {
// return new AppStore('');
// })
]);
var myTodoService = injector.get(TodosService)
答案 1 :(得分:0)
Tx到GünterZöchbaue为我解决了这个问题......他是男人!!!
这是其他人的代码:
constructor(private appStore:AppStore) {
super();
}
private factoryService(){
if (this.service)
return;
var injector = Injector.resolveAndCreate(
[
TodosService,
TodoAction,
HTTP_PROVIDERS,
TodoStatsModel,
provide(AppStore, {useValue: this.appStore})
]);
this.service = injector.get(TodosService);
service.sayHello('Sean');
}
基本上通过多个依赖项(甚至一个vanilla非组件(类)AppStore)手动创建“TodoService”Angular2组件,所有这些都通过注入器... wooof ...!