Angular 为什么我的路由器导航功能没有';你不能在8号房间工作吗?
我试图重定向到另一个组件,如下所示: HomeComponentAngular 为什么我的路由器导航功能没有';你不能在8号房间工作吗?,angular,angular-ui-router,angular2-routing,angular8,router,Angular,Angular Ui Router,Angular2 Routing,Angular8,Router,我试图重定向到另一个组件,如下所示: HomeComponent checkUrl(reference) { if (reference != this.ref) { this.router.navigate(['/еrror']); } } 这是我的路由器模块 const routes: Routes = [ { path: '', component: DefaultComponent }, { path: '', chil
checkUrl(reference) {
if (reference != this.ref) {
this.router.navigate(['/еrror']);
}
}
这是我的路由器模块
const routes: Routes = [
{ path: '', component: DefaultComponent },
{
path: '',
children: [
{ path: ':dlr/:id/:ref', component: HomeComponent },
{ path: 'error', component: ErrorPageComponent },
]
},
{ path: '**', redirectTo: '', pathMatch: 'full' }
];
现在我在HomeComponent中,想转到错误页面
this.router.navigate(['/öerror'])这将引导我找到DefaultComponent
将相对添加到这对我很有效。将详细信息添加到先前删除的答案- 由于它是一个子到子导航,您可以尝试添加
relativeTo:this.activatedRoute
标记。您可以在文档中找到有关其语法和用法的更多信息。这基本上类似于我们在尝试指定文件路径时通常用于导航的“../”类型的符号
因此,您可以尝试这样的操作-this.router.navigate(['/öerror'],{relativeTo:this.activatedRoute})代码>
此外,这里需要注意的一点是,如果您将使用['..error']而不是此['/error'],则导航可能会有所不同。例如,如果您当前在这里-parent/abc/c1
(其中c1是一个子组件),并且您使用['error']
(其中'error'是与c1相同的父组件的子组件),则URI将变为parent/abc/c1/error
,而如果您将使用['/error']
,您将首先向上移动一级parent/abc/
,然后添加另一个子级-parent/abc/error
这里还介绍了一些其他的方法——以及我上面介绍的方法,请看一看。
希望这有帮助。试试这个.router.navigate(['error'])
已经尝试过这种方法,结果是相同的。
this.router.navigate(['/error'], { relativeTo: this.activatedRoute });