在AngularDart中如何将PreEnter事件注册到需要使用服务方法的路由?

UCJ*_*ava 4 dart angular-dart

在AngularDart中的所有弃用之后,现在通过我的模块构造函数中的下面的initRoutes方法配置路由.

//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
...
Run Code Online (Sandbox Code Playgroud)

我的问题是如何在routeconfig.dart中注入具有_checkAuthentication方法的Service类?既然它不是一个类,怎么能在这里获得依赖注入?或者是否有另一种方法在Module contructor中初始化和注册RouteInitializerFn?

Mic*_*ski 7

您可以使用以下技术:可以使用'call'方法将函数实现为类.

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

  MyRouteInitializer(this.service);
  call(Router router, RouteViewFactory viewFactory) {
     ...
     service._checkAuthentication();
     ...
  }
}
Run Code Online (Sandbox Code Playgroud)

模块中的功能注册:

 this.bind(RouteInitializerFn, toImplementation: MyRouteInitializer);
Run Code Online (Sandbox Code Playgroud)