[英]How can I inject a string into a service factory?
I'm trying to inject a string into a service factory, but am running into error TS1206: Decorators are not valid here
. 我正在尝试将一个字符串注入服务工厂,但TS1206: Decorators are not valid here
错误TS1206: Decorators are not valid here
。
I'm using the following code - 我正在使用以下代码 -
export let serviceFactory = (http: Http, @Inject('TEMPLATE') template: string) => {
return new Service(http, template);
}
I use my factory like so - 我这样用我的工厂 -
export let serviceProvider = {
provide: Service,
useFactory: serviceFactory,
deps: [Http]
};
How can I inject a string in a service factory? 如何在服务工厂中注入字符串?
app.module.ts app.module.ts
export let serviceFactory = (http: Http, template: string) => {
return new Service(http, template);
}
export let serviceProvider = {
provide: Service,
useFactory: serviceFactory,
deps: [Http, [new Inject('TEMPLATE')]]
};
@NgModule({
imports: [
BrowserModule,
HttpModule
],
declarations: [ AppComponent ],
providers: [
{ provide: 'TEMPLATE', useValue: 'test' },
serviceProvider,
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
service.ts service.ts
@Injectable()
export class Service {
constructor(private http: Http, @Inject('TEMPLATE') template: string) {
console.log(template);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.