Angular 2 RC4 Router get intended route before activated

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 16:38:33

问题


I am trying to determine what the route is before it is activated so I can cache it and redirect the user back to that route after they have logged in.

In the old beta router I was able to call ComponentInstruction.routeName in the activate hook but in the newer canActivate() guard I do not see a way to access the intended route before it is activated.

I could store the intended route in a shared service when the user clicks on a navagtion button in my app but what about when they enter the URL in the address bar?


回答1:


From angular router source files:

export interface CanDeactivate<T> {
  canDeactivate(component: T,
   route:ActivatedRouteSnapshot,
   state:RouterStateSnapshot): Observable<boolean> | boolean;
}

ActivatedRouteSnapshot object (route) has url property - array of another type, in first object in property path your current path.

Also inside RouterStateSnapshot object (state) has just string property url with current path, but with / prefix on it.

Simply pass those arguments to your canDeactivate method )



来源:https://stackoverflow.com/questions/38392483/angular-2-rc4-router-get-intended-route-before-activated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!