作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 angular5 的简单应用程序,但出现以下错误:
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'list'
Error: Cannot match any routes. URL Segment: 'list'
我正在尝试编辑客户的信息,所以在我毫无问题地获得客户列表之前,我单击编辑,我正确地获得了他的所有信息但是当我保存时我应该看到客户列表,而不是我得到上述错误。
这是这个组件的结构:
文件:客户端--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 {}
最佳答案
使用相对导航代替从 list/edit/:id 到列表,使用 ../到你路径中的上一级
constructor( private route: ActivatedRoute,private router: Router) { }
this.router.navigate([ '../../../list' ], { relativeTo: this.route });
关于angular - 错误 错误 : Uncaught (in promise): Error: Cannot match any routes. URL 段 : 'list' Error: Cannot match any routes. URL 段: 'list',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50232927/
我是一名优秀的程序员,十分优秀!