首页 > 解决方案 > 角度路由不起作用,它将我重定向到基本网址

问题描述

我正在尝试使用angular router从我的页面导航。

我有一个button点击,我正在导航到其他页面。

我目前的网址是:http://localhost:4200/user/12345567。从这里我点击按钮导航离开

这是我的button代码:

leaveArea(){
    this.router.navigate([ 'account', 'dashboard'], {relativeTo: this.route});
}

我想导航到http://localhost:4200/account/dashboard点击。

但相反,我被导航到http://localhost:4200. 一瞬间(可能是一毫秒甚至更短),我确实看到了 url,就像http://localhost:4200/account/dashboard它导航到http://localhost:4200. 路由逻辑有问题吗?

编辑

我正在添加routeTrace结果:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

编辑

这是我的路由文件:

const appRoutes: Routes = [

    { path: "", component: HomeComponent },


    { path: "account/dashboard", component: AccountDashboardComponent},

    {
         path: "user/:id", component: UserComponent
    }
];

@NgModule({
    imports: [RouterModule.forRoot(appRoutes, { enableTracing: true })],
    exports: [RouterModule]
})
export class AppRoutingModule {}

标签: angular

解决方案


只需尝试router.navigateByUrl像这样使用 -

leaveArea(){
    this.router.navigateByUrl('/account/dashboard');
}

PS:通过附加/在字符串的开头将考虑从根级别路由。


推荐阅读