LoggedInOutlet angular2 authentication - Router v3.0.0-alpha8 - Where is ComponentInstruction?

后端 未结 2 1744
一个人的身影
一个人的身影 2021-01-22 03:08

I am using code like this to extend RouterOutlet and create app wide authentication and route protection

import {Directive, Attribute, ViewContainerRef, DynamicC         


        
相关标签:
2条回答
  • 2021-01-22 03:42

    ComponentInstruction has been deprecated. In the current RC4 version of Angular2, this class has been listed under reouter-deprecated. With RC5 coming in, this package would be dropped.

    RouterOutlet has changed a lot over time and to make your class LoggedInRouterOultet work, you have to use CanActivate interface.

    You can do something like this:

    Have an injectable service like LoggedInActivator shown here:

    import { Injectable } from '@angular/core';
    import { Router, CanActivate } from '@angular/router';
    import { LogInService } from './login.service';
    
    @Injectable()
    export class LoggedInActivator implements CanActivate {
      constructor(private loginService: LogInService) {}
    
      canActivate() {
        return this.loginService.isLoggedIn();
      }
    }
    

    Add canActivate and map it to LoggedInActivator on component while defining route:

    { path: 'home', component: HomeComponent, canActivate: [LoggedInActivator] }
    

    I hope this helps!

    0 讨论(0)
  • 2021-01-22 03:48

    because in new router, it uses CanActivate

    0 讨论(0)
提交回复
热议问题