2014-07-10 2 views
4

AngularDart의 모든 지원 중단 이후 이제 내 모듈 생성자 내에서 아래의 initRoutes 메소드를 통해 경로를 구성합니다.서비스 메소드를 사용해야하는 경로에 PreEnter 이벤트를 등록하는 방법은 무엇입니까?

//inside the main of the appliction 
.. 
this.bind(RouteInitializerFn, toValue:initRoutes); 
this.bind(NgRoutingUsePushState, toFactory:(_) => new NgRoutingUsePushState.value(false)); 

//in routeconfig.dart file which is imported in the main.dart 
void initRoutes(Router router, RouteViewFactory viewFactory) { 

viewFactory.configure({ 
      'loginPage': ngRoute(
      path: '/loginPage', 
      view: 'view/loginPage.html'), 
      'landingPage': ngRoute(
      path: '/landingPage', 
      view: 'view/landingPage.html', 
      defaultRoute: true, 
      enter: _checkAuthentication 
... 

내 질문 routeconfig.dart에서 _checkAuthentication 방법이있는 서비스 클래스를 주입하는 방법은? 클래스가 아니기 때문에 의존성 주입을 어떻게 얻을 수 있습니까? 또는 모듈 생성자에서 RouteInitializerFn을 초기화하고 등록하는 또 다른 방법이 있습니까?

답변

7

다음 기술을 사용할 수 있습니다. 함수는 'call'메서드를 사용하여 클래스로 구현할 수 있습니다. 모듈의

@Injectable() 
class MyRouteInitializer implements Function { 
    AuthService service; 

    MyRouteInitializer(this.service); 
    call(Router router, RouteViewFactory viewFactory) { 
    ... 
    service._checkAuthentication(); 
    ... 
    } 
} 

기능 등록 :

this.bind(RouteInitializerFn, toImplementation: MyRouteInitializer); 
+0

덕분에 트릭을 수행하는 많은! – UCJava