简体   繁体   English

如何在 Angular2 中使用动态默认路由?

[英]How to use dynamic default route in Angular2?

Problem: Depending on user's permission another default route should be used.问题:根据用户的许可,应该使用另一个默认路由。

@RouteConfig([
    { path: '/x1', name: 'x1', component: X1Component, useAsDefault: true },
    { path: '/x2', name: 'x2', component: X2Component},
    { path: '/x3', name: 'x3', component: X3Component},
    { path: '/x4', name: 'x4', component: X4Component},
    { path: '/x5', name: 'x5', component: X5Component}
])

Given x1 as a default route in above RouteConfig but current user does not have permission to access this page, x2 should be used as default route if permission will be granted and so on.给定x1作为上述RouteConfig的默认路由,但当前用户没有访问此页面的权限,如果授予权限,则应使用x2作为默认路由等。

Anyway, we've tried the attribute loader already.无论如何,我们已经尝试了属性loader The problem there is, that the url won't be updated (eg from /x1 to x2 ) and this occurs other problems such as not having the css class router-link-active automatically attached to a link in our menu.问题是,url 不会更新(例如从/x1x2 ),这会发生其他问题,例如没有将 css 类router-link-active自动附加到我们菜单中的链接。

Of course we could write an workaround but how are you solving this kind of problem?当然,我们可以编写一个解决方法,但是您如何解决此类问题?

I don't think changing which route is the default route is possible at runtime.我认为在运行时更改默认路由的路由是不可能的。

I think a common approach would be to add a @CanActivate() decorator to your components that require login and if the user is not actually logged in, redirect to the appropriate route.我认为一种常见的方法是向需要登录的组件添加@CanActivate()装饰器,如果用户实际上未登录,则重定向到适当的路由。

To know whether the current user is logged in you probably want to use DI to get global information about the user state.要知道当前用户是否已登录,您可能希望使用 DI 来获取有关用户状态的全局信息。
You also need a reference to the root router instance which you can get from DI.您还需要对可以从 DI 获取的根路由器实例的引用。 Using DI in @CanActivate() is currently not directly supported but the discussion in the related Angular2 issue provides a workaround (with Plunker links) to get a reference to the global injector.目前不直接支持在@CanActivate()使用 DI,但相关 Angular2 问题中的讨论提供了一种解决方法(使用 Plunker 链接)来获取对全局注入器的引用。

See also Redirect to a different component inside @CanActivate in Angular2另请参阅在 Angular2 中重定向到@CanActivate 中的不同组件

To change the default route of an Angular application dynamically, at the moment I am using the following approach.要动态更改 Angular 应用程序的默认路由,目前我正在使用以下方法。

The last fallback route with a guard:带守卫的最后一条后备路线:

...
{
    path: '**',
    canActivate: [DynamicDefaultRouteGuard],
    pathMatch: 'full'
}

The guard is the following:守卫如下:

@Injectable({
    providedIn: 'root'
})
export class DynamicDefaultRouteGuard implements CanActivate {
    constructor(private router: Router, private dynamicDefaultMenuSectionService: DynamicDefaultMenuSectionService) {}

    canActivate(
        next: ActivatedRouteSnapshot,
        state: RouterStateSnapshot
    ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
        console.warn(
            `Fallback main module route reached. Redirecting to ${this.dynamicDefaultMenuSectionService.defaultMenuSection}...`
        );

        return this.router.createUrlTree([this.dynamicDefaultMenuSectionService.defaultMenuSection]);
    }
}

where dynamicDefaultMenuSectionService.defaultMenuSection is a variable with a default route.其中dynamicDefaultMenuSectionService.defaultMenuSection是一个带有默认路由的变量。 This variable is initialized during app initialization (APP_INITIALIZER) but it can be changed any time.此变量在应用程序初始化 (APP_INITIALIZER) 期间初始化,但可以随时更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM