我有一个通用的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
可能是什么问题?谢谢
答案 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
是否在声明中。