必须对RC4

时间:2016-09-01 09:12:11

标签: javascript angular typescript

我在Beta中运行以下引导代码:

bootstrap(App, [
    provide(Http, {
        useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, helperService: HelperService, authProvider: AuthProvider) => new CustomHttp(backend, defaultOptions, helperService, authProvider),
        deps: [XHRBackend, RequestOptions, HelperService, AuthProvider]
    }),
    provide(LocationStrategy, { useClass: HashLocationStrategy }),
    provide("AppContext", { useValue: appContext }),
    provide("ClientService", { useValue: clientService }),
    AgendaService,
    ConfirmationService,
    HelperService
]).then((success: any) => {
        console.log("Bootstrap successful");
    }, (error: any) => console.error(error));

但是现在更新到RC4时,我必须更改为以下内容:

bootstrap(App, [
    provide(Http, {
        useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, helperService: HelperService, authProvider: AuthProvider) => new CustomHttp(backend, defaultOptions, helperService, authProvider),
        deps: [XHRBackend, RequestOptions, HelperService, AuthProvider]
    }),
    provide(LocationStrategy, { useClass: HashLocationStrategy }),
    provide("AppContext", { useValue: appContext }),
    provide("ClientService", { useValue: clientService }),
    provide("AgendaService", { useClass: AgendaService }),
    provide("ConfirmationService", { useClass: ConfirmationService }),
    provide("HelperService", { useClass: HelperService })
]).then((success: any) => {
        console.log("Bootstrap successful");
    }, (error: any) => console.error(error));

所以,我必须为我的每个服务使用provide(),并在注入这些服务的组件中使用Inject(),否则无法注入它们,我得到NoProviderError消息No provider for xxx.service。这是预期还是我做错了什么?

似乎有效的是:

bootstrap(App, [
    // provide(Http, {
    //     useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, helperService: HelperService, authProvider: AuthProvider) => new CustomHttp(backend, defaultOptions, helperService, authProvider),
    //     deps: [XHRBackend, RequestOptions, HelperService, AuthProvider]
    // }),
    { provide: LocationStrategy, useClass: HashLocationStrategy },
    { provide: "AppContext", useValue: appContext },
    { provide: "ClientService", useValue: clientService },
    { provide: "AgendaService" useClass: AgendaService },
    { provide: "ConfirmationService" useClass: ConfirmationService },
    { provide: "HelperService" useClass: HelperService }
]).then((success: any) => {
        console.log("Bootstrap successful");
    }, (error: any) => console.error(error));

然后,必须使用这些服务的所有地方@Inject

constructor(@Inject("HelperService") private helperService: HelperService){ }

其他任何事情都像以前一样给出错误。

注意:我正在使用useValue而不是useClass来获取某些服务,因为我根据某些条件手动实例化这些服务,并在引导Angular之前完成,在bootstrap()之上。< / p>

2 个答案:

答案 0 :(得分:1)

provide()(以及new Provider())`已弃用,已在RC.6中删除

最简单的方法就是

AgendaService

这是

的缩写形式
{provide: AgendaService, useClass: AgendaService}

如果密钥和值不同,那么需要使用这个长格式,如果没有,则更喜欢简短格式,因为它更容易阅读。

此处需要长格式,因为键LocatonStrategy与值HashLocationStrategy不同:

{ provide: LocationStrategy,  useClass: HashLocationStrategy }),

答案 1 :(得分:1)

您不需要为每项服务使用provide。来自provide的{​​{1}}也已弃用,因此您需要使用提供商,如下所示:

@angular/core

还要确保为AgendaService,ConfirmationService和HelperService添加了bootstrap(App, [ { provide : Http, useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, helperService: HelperService, authProvider: AuthProvider) => new CustomHttp(backend, defaultOptions, helperService, authProvider), deps: [XHRBackend, RequestOptions, HelperService, AuthProvider] }, { provide : LocationStrategy, useClass: HashLocationStrategy }, { provide : AppContext, useValue: appContext }, { provide : ClientService, useValue: clientService }, AgendaService, ConfirmationService, HelperService ]).then((success: any) => { console.log("Bootstrap successful"); }, (error: any) => console.error(error));

例如对于AgendaService,如下所示:

@Injectable()