Angular2:在延迟加载的模块上未解析OpaqueToken

时间:2017-02-16 15:48:23

标签: angular

此帖子中的所有代码均在this github repository上。为了测试它,请执行npm start并访问http://localhost:3002

我创建了一个库,我在其中声明了几个服务:

@Injectable()
export class UsersService {
    protected basePath = 'http://localhost/commty/cmng';

    constructor(@Optional()@Inject(BASE_PATH) basePath: string) {
        if (basePath) {
            this.basePath = basePath;
        }
    }
}

在这个库上,我使用OpakeToken来获取UsersService的{​​{1}}构造函数参数。此参数在basePath上定义为:

variables.ts

在这个库上,我创建了一个名为export const BASE_PATH = new OpaqueToken('basePath'); 的模块:

ApiModule

我已经发布了这个库,它在我的@NgModule({ imports: [ CommonModule, HttpModule ], declarations: [], exports: [], providers: [ PlansService, UsersService ] }) export class ApiModule {} webapp上用作简单的npm依赖包。

在我的"@living/cest": "0.1.0-unstable0028" 上,我创建了一个webapp文件,以设置此environtment.ts

OpaqueToken

我正在使用ENV_PROVIDERS = [ { provide: BASE_PATH, useValue: 'http://localhost:8082/commty/cmng' } ]; 来使用回流模型创建我的内部行为。我创建了三个模块:ngrxAppModuleCoreModule

SharedModule

所以,我在import { ENV_PROVIDERS } from './environment'; @NgModule({ bootstrap: [ AppComponent ], declarations: [ AppComponent, ErrorComponent ], imports: [ BrowserModule, CoreModule, <<<<<<<<<<<<<<<< SharedModule, <<<<<<<<<<<<<<<< RouterModule.forRoot(ROUTES, { useHash: true }) ], providers: [ ENV_PROVIDERS ] }) export class AppModule { ... } 设置ENV_PROVIDERSBASE_PATH不透明令牌提供商),我正在导入AppModuleCoreMoudle

我在所有SharedModule s中使用UsersService

@Effect

每个@Injectable() export class UserEffects { constructor( private _actions$: Actions, private _store$: Store<IStore>, private _userService: UsersService, ) { } @Effect({ dispatch: true }) userLogin$: Observable<Action> = this._actions$ .ofType('USER_REDUCER_USER_LOGIN') .switchMap((action: Action) => this._userService.checkPasswd(action.payload.username, action.payload.password) .map((user: any) => { return { type: 'USER_REDUCER_USER_LOGIN_SUCCESS', payload: user }; }) .catch((err) => { return Observable.of({ type: 'USER_REDUCER_USER_LOGIN_ERROR', payload: { error: err } }); }) ); } 都在@Effect上引导:

CoreModule

如您所见,我在@NgModule({ imports: [ ApiModule, StoreModule.provideStore(getRootReducer), StoreDevtoolsModule.instrumentOnlyWithExtension(), EffectsModule.runAfterBootstrap(UserEffects) ], }) export class CoreModule { } 中导入ApiModule一次。此CoreModule会在CoreModule中导入一次。

另一方面AppModule代表向SharedModule的所有组件提供ngrx功能:

webapp

我真的检查了所有内容,但export const modules = [ StoreModule, StoreDevtoolsModule ]; export const declarations = []; @NgModule({ imports: modules, exports: [...modules, ...declarations], declarations }) export class SharedModule { } 的{​​{1}} constrcutor参数未解析为我的网址,它总是UsersService

我看了一下调试器,以找出问题所在。到目前为止,我已经能够看到angular正在尝试解析basePath的{​​{1}}构造函数参数injectable。到达此代码:

null

此外,此代码已生成但从未到达:

UsersEffect

_userService使用此路由器配置延迟加载:

Object.defineProperty(AppModuleInjector.prototype, '_UserEffects_77', { get: function() {
  var self = this;
  if ((self.__UserEffects_77 == null)) { (self.__UserEffects_77 = new jit_UserEffects63(self._Actions_60,self._Store_62,<b>self._UsersService_47</b>)); }
  return self.__UserEffects_77;
}});

Object.defineProperty(AppModuleInjector.prototype, '_Token_basePath_81', { get: function() { var self = this; if ((self.__Token_basePath_81 == null)) { (self.__Token_basePath_81 = 'http://localhost:8082/commty/cmng'); } return self.__Token_basePath_81; }}); LoginModule被急切加载。

我希望我解释得那么好。

修改

我还尝试删除export const ROUTES: Routes = [{ path: '', redirectTo: 'login', pathMatch: 'full' }, { path: 'login', loadChildren: () => System.import('./login/login.module') }, { path: 'error', component: ErrorComponent }, { path: '**', component: ErrorComponent } ]; SharedModule并导入CoreModule上的所有内容。然而,CoreMoudule不透明令牌会保持faling以解析任何值:

SharedModule

0 个答案:

没有答案
相关问题