Angular 10-导航路由器无法正常工作

时间:2020-09-28 11:02:30

标签: angular

我有一个通用的Angular路由配置:

const routes: Routes = [
  {
    path: `students`, loadChildren: () =>
      import('./@pages/students/students.module').then(m => m.StudentModule),
  },
  { path: ``, redirectTo: `mainPage`, pathMatch: `full` }
];

还有一个用于“ StudentsModule”模块的路径文件:

const routes: Routes = [
  {
    path:  ``, 
    component: StudentsComponent,
  },
  {
    path:  `students/:idstudent`, 
    component: StudentsTableComponent,
  },
];

当我单击组件模板“ StudentsComponent”的按钮时,我想去

this.router.navigate(['/students/' + this.idStudent]);

但这给了我以下错误:

core.js:4197 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'students/3'
Error: Cannot match any routes. URL Segment: 'students/3'

http:// localhost:4200 / students,加载组件:StudentsComponent

http:// localhost:4200 / students / 3,应该正在加载组件:StudentsTableComponent

可能是什么问题?谢谢

1 个答案:

答案 0 :(得分:1)

您可以这样使用

 this.router.navigate(['/students/' + this.idStudent]);

OR

 this.router.navigate([`/students/${this.idStudent}`]);

路线:StudentsModule

const routes: Routes = [
  {
    path:  ``, 
    component: StudentsComponent,
  },
  {
    path:  `:idstudent`, 
    component: StudentsTableComponent,
  },
];

如果无法解决此问题,请检入StudentsModule文件StudentTableComponent是否在声明中。

相关问题