我有一个使用angular5的简单应用程序,但我收到以下错误:
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'list'
Error: Cannot match any routes. URL Segment: 'list'
我正在尝试编辑客户端的信息,所以在我得到没有问题的客户端列表之前,我点击编辑,我得到了正确的所有信息但是当我保存时我应该看到客户列表,而不是我得到上述错误。
这是该组件的结构:
文件:client - routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {ClientsComponent} from './clients/clients.component';
import {EditClientsComponent} from './clients/edit-clients/edit-clients.component';
import {NewClientsComponent} from './new-clients/new-clients.component';
const routes: Routes = [
{
path: '',
pathMatch: 'full',
data: {
title: 'Clients'
}
},
{
path: 'list',
pathMatch: 'full',
component: ClientsComponent,
data: {
title: 'Liste des clients'
}
},
{
path: 'list/edit/:id',
pathMatch: 'full',
component : EditClientsComponent,
data : {
title: 'editer un client'
}
},
{
path: 'ajouter',
pathMatch: 'full',
component : NewClientsComponent,
data: {
title: 'Ajouter un client'
}
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ClientRoutingModule {}
文件EditClientComponent.ts
import { Component, OnInit } from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Clients} from '../../../Models/Clients';
import {ClientService} from '../../../../../../service/client.service';
@Component({
selector: 'app-edit-clients',
templateUrl: './edit-clients.component.html',
styleUrls: ['./edit-clients.component.scss']
})
export class EditClientsComponent implements OnInit {
idClient:number;
client:Clients = new Clients();
constructor(public activatedRoute:ActivatedRoute ,
public clientService:ClientService,
public router:Router) {
this.idClient = activatedRoute.snapshot.params['id'];
}
ngOnInit() {
this.clientService.getClient(this.idClient)
.subscribe((data:Clients)=>{
this.client=data;
},err=>{
console.log(err);
})
}
EditClient(){
this.clientService.updateClient(this.client)
.subscribe(data=>{
console.log(data);
this.router.navigateByUrl('list');
},err=>{
console.log(err);
alert("Probleme");
})
}
}
最终app.routinge.ts
export const routes: Routes = [
{
path: '',
redirectTo: 'pages',
pathMatch: 'full',
},
{
path: '',
component: FullLayoutComponent,
data: {
title: 'Home'
},
children: [
{
path: 'GestionClients',
loadChildren: './views/Admin/GestionClients/client.module#ClientModule'
}
]
}
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
答案 0 :(得分:2)
使用相对导航来代替从list / edit /:id到list,使用../来在你的路径中上升一级
constructor( private route: ActivatedRoute,private router: Router) { }
this.router.navigate([ '../../../list' ], { relativeTo: this.route });
答案 1 :(得分:0)
在我的情况下,我在AppRoutingModule
中使用了错误的名称app.module.ts
,Visual Studio Code没有显示任何错误,也没有显示任何编译错误:>
import { AppRoutingModule } from './app-routing.module';
请确保使用正确的名称,就我而言,就像这样:
import { AppRoutingModule } from './app.routing.module';
希望这会有所帮助。