在 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/