在访问ActivatedRouteSnapshot 实例中的data属性时,我遇到了 Angular 路由的问题。
如果我通过单击链接按钮,路线数据可用,但如果直接在浏览器中键入 url 或刷新页面,则路线数据不可用。
RouterModule 路由:
const routes: Routes = [
{
path: '',
canActivate: [AuthGuard],
component: DefaultLayoutComponent,
children: [
{
path: 'usermanagement/userdetails/:username',
canActivate: [AuthGuard],
data: { parenturl: 'usermanagement/users', permissions: ["canView", "canAdd", "canEdit"] },
component: UserDetailsComponent,
canDeactivate: [CanDeactivateGuard]
}
]
},
];
身份验证:
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
constructor(
private router: Router,
private activeRoute: ActivatedRoute,
private accountService: AccountService
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
var path = route.routeConfig?.path; // not found after refress
var permissions = route.data["permissions"] // not found after refress
return false;
}
}