2017-12-14 7 views
0

UIルータのRootModule.otherwiseでは、ユーザがアプリケーションが認識しないURLを開いている場合に、ユーザをリダイレクトする場所を指定できます。私は、単純な形式、対象の状態の名前を指定するだけで文字列を使用してきました:UIルータの `otherwise`関数のサービスへのアクセス

@NgModule({ 
    imports: [UIRouterModule.forRoot({ 
     states: [ 
      { 
       name: "home", 
       url: "/home", 
       component: HomeComponent 
      } 
     ], 
     otherwise: "/home" 
    })], 
    exports: [UIRouterModule], 
    providers: [] 
}) 
export class AppRoutingModule { } 

をしかし、今、私はユーザーをリダイレクトすべき対象の状態を判断するために、サービスを呼び出すことで、データを取得する必要があります。私は、インジェクタのホールドを取得する方法を見つけることができない、

otherwise: (matchValue, url: UrlParts, router: UIRouter) => { 
    // this didn't work, router.globals.transition is not available 
    const userPrefSvc = router.globals.transition.injector().get(UserPreferenceService); 
} 

しかし、それは動作しませんでした:私は、インジェクタを通じて、私のサービスへのアクセスを得ることができることを望んでotherwiseの関数形式を試してみました。

otherwise機能からサービスにアクセスできますか?

答えて

1

この回答は実際にはChristopher Thielenです。

@NgModule({ 
    imports: [UIRouterModule.forRoot({ 
     states: [ 
      { 
       name: "home", 
       url: "/home", 
       component: HomeComponent 
      } 
     ], 
     config: (uiRouter: UIRouter, injector: Injector, statesModule: StatesModule) => { 
      let userPrefSvc = injector.get(UserPreferenceService); 

      uiRouter.urlService.rules.otherwise((matchValue, url, router) => { 
       // ... 
       router.stateService.go("home"); 
       // or 
       return "/home"; 
      }); 
     } 
    })], 
    exports: [UIRouterModule], 
    providers: [] 
}) 
export class AppRoutingModule { } 
otherwise機能を設定

はまた、我々は、インジェクタへのアクセス権を持っている設定機能、で行うことができます

関連する問題