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

标签 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
...

我的问题是如何在routeconfig.dart中注入(inject)具有_checkAuthentication方法的Service类?既然它不是一个类,如何在这里获得依赖注入(inject)?或者是否有另一种方法可以在模块构造函数中初始化和注册 RouteInitializerFn ?

最佳答案

您可以使用以下技术:可以使用“call”方法将函数实现为类。

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

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

模块中的函数注册:

 this.bind(RouteInitializerFn, toImplementation: MyRouteInitializer);

关于dart - 在AngularDart中如何将PreEnter事件注册到需要使用服务方法的路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24683944/

相关文章:

list - 有没有办法在特定索引处将值插入 EMPTY 列表

dart - 应用程序启动后如何启动一次登录和启动画面页面

asynchronous - 从 Dart 中的构造函数调用异步方法

postgresql - Dart Aqueduct服务器数据库升级:连接数据库时出错

flutter - 刷新页面时AutoCompleteTextField检索null

dart - Dart for Web REST API登录示例

dart - 太多导入导致 `pub serve` 挂起

dart - AngularDart 中的 somecomponent.template.dart 导入指向什么?

filter - 无法让 AngularDart 过滤工作

javascript - Angular ngFor 调用 javascript