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

前端 未结 5 404
误落风尘
误落风尘 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:09

    These answers are no longer valid in the RC candidate as of 22/6/16.

    There is a lot of talk a new solution for @CanActivate annotation but you can revert back to using the angular-2/router-deprecated version in the interim.

    If you want to follow the updates to this area if you want to use the newer router implementation please check out these two github issues:

    • Router OnActivate does not delay render until promise settles #6611
    • Equivalent to resolve in Component Router? #4015

    Sorry there is no complete answer right now, if I see any further progress on this I'll update this reply. I'm searching for an elegant solution as well.

    For the interim I just used a ngIf='fooObject' in a div to check for object being used in a child directive is populated/truthy and then render the rest of the components html. It's not ideal but it does work consistently right now. May help you or not depending on your use case. I don't need to redirect, only check my data has resolved for the display of my component.

提交回复
热议问题