Modele构造函数调用了两次

时间:2017-01-09 12:13:56

标签: angular

我有一个带有2个静态函数的模块forRoot()和forChild()但是当调用子代时

TranslateModule.forChild({
  provide: TranslateLoader,
  useFactory: translateLoaderFactory,
  deps: [Http]
})

我的模块构造函数在我的模块应用程序中调用forChild并且在我的app.module中调用一次时被调用两次为什么是原因?

    constructor(public loader: ModuleLoader) {
          this.loader.init();
    }
    static forRoot(providedLoader: any = {
        provide: TranslateLoader,
        useFactory: translateLoaderFactory,
        deps: [Http]
    }): ModuleWithProviders {
        return {
            ngModule: TranslateModule,
            providers: [
                providedLoader,
                ModuleLoader,
                { provide: ModuleIdentifier, useValue: {uid: 'root'} },
                TranslateService,
                { provide: TranslateParser, useClass: DefaultTranslateParser }, 
            ]
        };
    }
    static forChild(providedLoader: any = {
        provide: TranslateLoader,
        useFactory: translateLoaderFactory,
        deps: [Http]
    }): ModuleWithProviders {
        let uid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
          function(c: string) {
              var r = Math.floor(Math.random() * 16),
                  v = c === 'x' ? r : (r % 4 + 4);
              return v.toString(16);
          }).toUpperCase();
        console.log("xxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx", uid);
        return {
            ngModule: TranslateModule,
            providers: [
                providedLoader,
                ModuleLoader,
                { provide: ModuleIdentifier, useValue: {uid: uid}}, 
            ]
        };
    }
}

0 个答案:

没有答案