3

我有一个使用 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 {}
4

2 回答 2

4

使用相对导航从 list/edit/:id 转到列表,使用 ../ 在您的路径中上一级

 constructor( private route: ActivatedRoute,private router: Router) { } 

 this.router.navigate([ '../../../list' ], { relativeTo: this.route });
于 2018-05-08T12:00:16.743 回答
1

在我的情况下,我有一个错误的名称AppRoutingModule,在 中app.module.ts,Visual Studio Code 没有显示任何错误,并且我没有收到任何编译错误:

import { AppRoutingModule } from './app-routing.module';

确保使用正确的名称,在我的情况下是这样的:

import { AppRoutingModule } from './app.routing.module';

希望这会有所帮助。

于 2018-12-30T21:29:32.313 回答