Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in AngularDart how to register a PreEnter event to a route which needs to use a service method?

After all the deprecation in AngularDart , now configuring routes via the below initRoutes method inside my modules constructor.

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

My question is how to inject the Service class which has the _checkAuthentication method in routeconfig.dart ? Since it's not a class how can get the dependency injection here ? or is there another way to initialize and register the RouteInitializerFn in the Module contructor ?

like image 300
UCJava Avatar asked Mar 20 '23 02:03

UCJava


1 Answers

You can use the following technique: functions can be implemented as classes with the 'call' method.

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

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

Function registration in the module:

 this.bind(RouteInitializerFn, toImplementation: MyRouteInitializer);
like image 191
Michal Pietrusinski Avatar answered Mar 22 '23 05:03

Michal Pietrusinski