Angular2 Router - Anyone know how to use canActivate in app.ts so that I can redirect to home page if not logged in

前端 未结 5 418
误落风尘
误落风尘 2020-12-05 12:06

Angular2 Router - Anyone know how to use canActivate in app.ts so that I can redirect to home page if not logged in

I\'m using typescript and angular 2.

5条回答
  •  醉酒成梦
    2020-12-05 12:12

    From here:

    https://angular.io/docs/ts/latest/api/router/CanActivate-decorator.html

    CanActivate is an annotation, not a function. You use it like this:

    @Component({selector: ... })
    @CanActivate(()=>console.log('Should the component Activate?'))
    class AppComponent {
    
    }
    

    routerOnActivate IS a function you use inside your component:

    https://angular.io/docs/ts/latest/api/router/OnActivate-interface.html

提交回复
热议问题