How to inject services into RouteInitializerFn (new routing DSL)

后端 未结 1 1165
难免孤独
难免孤独 2021-01-12 09:01

I\'m switching my app over to the new routing DSL. Specifically, I want to do something like this with preEnter:



        
相关标签:
1条回答
  • 2021-01-12 09:17

    I found a pretty cool solution to this problem. Turns out, if you define a call method on a class that satisfies a typedef, you can configure it as an implementation of the typedef. Very cool. Here is my solution:

    class Routes
    {
      final UserService _userService;
    
      Routes(this._userService);
    
      void call(Router router, ViewFactory views) 
      {
        views.configure({
          'chat': ngRoute(
              path: '/chat',
              preEnter: (RoutePreEnterEvent e) => e.allowEnter(this._userService.requireUserState(UserService.LOGGED_IN)),
              view: 'views/chat.html'
          ),
    
          'login': ngRoute(
              path: '',
              defaultRoute: true,
              view: 'views/login.html'
          )
        });
      }
    }
    

    and this is how it's configured within the module:

    // old syntax
    type(RouteInitializerFn, implementedBy: Routes);
    
    // new syntax
    bind(RouteInitializerFn, toImplementation: Routes);
    
    0 讨论(0)
提交回复
热议问题