Angular2 路由 : redirectTo a relative url

标签 angular angular2-routing

一切都在标题中:您可以使用 redirectTo 属性重定向到相对 URL 吗?

我的(简化的)路由如下:

{path: 'login', children: []},
{path: '', children: [ // made here for future guards
    {path: 'admin', children: [
        {path: 'dashboard', children: []}
        {path: '', redirectTo: 'dashboard'}
    ]}
]},
{path: '', redirectTo: 'login', pathMatch: 'full'}

我的路由运行良好,除了当我想访问 URL/admin/ 时出现错误,提示仪表板未知。但是,使用 redirectTo: 'admin/dashboard' 路由有效。

那么有没有办法通过 rediretcTo 使用相对路由?

编辑 整条路线:

app.module

{ path: 'login', component: LoginComponent, children: [] },
{ path: 'signup', component: SignupComponent, children: [] },
{ path: '', redirectTo: '/login', pathMatch: 'full' },
...loggedRoutes

logged.module

{ path: 'logged', component: LoggedComponent, children: [
{ path: 'profile', component: ProfileComponent, children: [] },
    ...adminRoutes,
    ...mainRoutes
] },

admin.module

{ path: 'admin', component: AdminComponent, children: [
    { path: 'dashboard', component: DashboardComponent, children: [] },
    { path: 'validation/:objectid', component: ValidationComponent, children: [] },
    { path: '', redirectTo: '/logged/admin/dashboard' },
] },

ma​​in.module

{ path: 'main', component: MainComponent, children: [
    { path: 'error', component: ErrorComponent, children: [] },
    { path: 'last', component: LastComponent, children: [] },
    { path: 'process', component: ProcessComponent, children: [] },
    { path: 'crt', component: CrtComponent, children: [] },
    { path: '', redirectTo: '/logged/main/error' },
] },

最佳答案

您只需将 pathMatch: 'full' 添加到您的重定向路由中,它就会按照您的预期工作。

{path: 'login', children: []},
{path: '', children: [ // made here for future guards
    {path: 'admin', children: [
        {path: 'dashboard', children: []}
        {path: '', redirectTo: 'dashboard', pathMatch: 'full'} // <-- ADDED here
    ]}
]},
{path: '', redirectTo: 'login', pathMatch: 'full'}

关于Angular2 路由 : redirectTo a relative url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41489358/

相关文章:

javascript - Angular 2 中组件模板之外的 routerLink,在基本 html 中

angular - 如何读取组件中的http状态代码错误

Angular2 - 'router-outlet' 不是已知元素

angular - 在不编码的情况下将字符串作为路由参数传递给 Angular

angular - "No provider for String"服务测试中

angular - 如何根据从 docker-compose.yml 传递的环境变量修改 Dockerfile RUN 命令的效果

javascript - 动态创建的元素上的 addEventListener 不起作用

javascript - Angular 组件返回未定义

angular - typescript 如何在没有引用的情况下复制对象

javascript - 通过 Angular 2 中的所有组件加载脚本