Angular2路由:重定向到相对URL

3

标题已经说明了一切:你能使用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'则可以正常运行路由。
那么有没有办法在redirectTo中使用相对路由呢? 编辑 整个路由如下: 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' },
] },

main.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' },
] },
1个回答

2

你只需要在重定向路由中添加 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'}

我已经尝试过那个方法,但没有成功。显然,当添加pathMatch时,URL需要完全等于参数。我希望像前缀一样有一个“后缀”,但不幸的是,情况并非如此。 - user4676340
你介意把你的整个路由数组发出来吗?对我来说,它是这样工作的...但也许是因为你在同一级别上有两个空路径选项,尝试将“admin”路径放在与“login”相同的级别上。将来你也可以在那里添加你的守卫。 - benny_boe
1
路径:'',重定向到:'仪表盘' - 它可以工作(Angular7) - Vasiliy Mazhekin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接