我正在使用routerLink返回我的页面。
当前路由可以有3个级别:
myurl.com/parent
myurl.com/parent/child
myurl.com/parent/child/grandchild另外,我有一些不同的组件,所以它不会总是父级,它可以是parent1或parent2,也可以是子级和孙级,例如:
myurl.com/parent2/child1/grandchild2我想要的是始终转到上一级,例如:
myurl.com/parent/child/grandchild -> myurl.com/parent/grandchild我所做的是:
<button [routerLink]="['../'']">
back
</button>但它总是导航到"myurl.com“
我该怎么解决它呢?
正如一些用户建议的那样,我正在分享我的路由配置:
在app.routing.ts中:
const APP_ROUTES: Routes = [
{
path: '',
canActivate: [LoggedInGuard],
children: [
{path: '', redirectTo: '/mainPage', pathMatch: 'full'},
{path: 'parent', loadChildren: 'app/parent/parend.module#ParentModule'
]},
{path: '**', redirectTo: ''}
];在parent.routing.ts中:
const PARENT_ROUTES: Routes = [
{path: '', component: ParentComponent},
{path: ':id', component: ChildComponent},
];如果我在浏览器中写道:
myurl.com/parent -> it works但是单击back按钮会失败
发布于 2018-07-11 22:57:26
如果您试图“向上”导航一个级别,那么您几乎是正确的。
<a routerLink="..">Up</a>这将导航
myurl.com/parent/child/grandchild -> myurl.com/parent/child然而,如果你想真正像浏览器的后退按钮那样导航“后退”,你需要使用javascript。
对于这个问题有多种现有的解决方案on this StackOverflow question
对于任何更复杂的事情,它将取决于您试图做什么-但它可能需要一个单击处理程序和一些手动操作的URL。
发布于 2021-02-03 11:17:50
试试这个[routerLink]="['../child']"
发布于 2020-11-25 10:57:32
当它是一个延迟加载的模块,并且是a known bug时,会特别注意到这一点。
Angular 11:开箱即用,运行良好。
@NgModule({
imports: [RouterModule.forRoot(routes, {})], // relativeLinkResolution === 'corrected' by default
exports: [RouterModule],
})
export class AppRoutingModule {}Angular 11之前的:显式使用{relativeLinkResolution: 'corrected'}。
@NgModule({
imports: [RouterModule.forRoot(routes, {relativeLinkResolution:'corrected'})],
exports: [RouterModule],
})
export class AppRoutingModule {}您还可以设置{relativeLinkResolution:'legacy'}来获取您所面临的当前行为。
https://stackoverflow.com/questions/51288503
复制相似问题