简体   繁体   English

错误:无法匹配 Angular 5 中的任何路由

[英]Error: Cannot match any routes in Angular 5

As per this Stack Overflow thread ( Cannot match any routes ), my problem is same but unfortunately I am unable to sort out.根据这个 Stack Overflow 线程( 无法匹配任何路由),我的问题是一样的,但不幸的是我无法解决。 The only difference is that my start screen is Login.唯一的区别是我的开始屏幕是登录。 Main screen have SideNav in which each item contains its path name.主屏幕有SideNav ,其中每个项目都包含其路径名。

app-routing.module.ts应用程序路由.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Router, Routes } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { MainComponent } from './main/main.component';
import { AuthguardService } from './authguard.service';
import { SelectpatientComponent } from
    './selectpatient/selectpatient.component';
import { MonitorPatientComponent } from './monitor-patient/monitor-patient.component';

const routes: Routes = [
    {
        path: 'main', component: MainComponent, canActivate: [AuthguardService],
        children: [
            { path: '', canActivate: [AuthguardService], component: SelectpatientComponent },
            {
                path: 'selectpatient', canActivate: [AuthguardService], component: SelectpatientComponent,
                pathMatch: 'full'
            },
            {
                path: 'monitorpatient', canActivate: [AuthguardService], component: MonitorPatientComponent,
                pathMatch: 'full'
            }

        ]
    },
    { path: '', component: LoginComponent, pathMatch: 'full' },
];

@NgModule({
    imports: [
        RouterModule.forRoot(routes)
    ],
    exports: [
        RouterModule
    ],
    providers: []
})
export class AppRoutingModule { }

main.component.html main.component.html

 <div class="example-container">
    <mat-toolbar class="example-toolbar">
        <button mat-icon-button (click)="snav.toggle()">
            <mat-icon>menu</mat-icon>
        </button>
        <h1 class="example-app-name">Select Patient</h1>
    </mat-toolbar>

    <mat-sidenav-container class="example-sidenav-container">
        <mat-sidenav #snav mode="over" fixedTopGap="56">
            <mat-nav-list>
                <app-menu-list-item *ngFor="let item of navItems" [item]="item"></app-menu-list-item>
            </mat-nav-list>
        </mat-sidenav>
        <router-outlet></router-outlet>
    </mat-sidenav-container>
</div>

The mat-sidenav contains nav items that have route name. mat-sidenav包含具有路由名称的导航项。 nav-item.ts导航项.ts

export interface NavItem {
    displayName: string;
    disabled?: boolean;
    iconName: string;
    route?: string; // <- Contains Route name
    children?: NavItem[];
}

And this is how I am navigating on nav item click:这就是我在导航项点击上导航的方式:

if (!item.children || !item.children.length) {
  this.router.navigate([item.route]);
  //this.navService.closeNav();
  console.log("onItemSelected");
}

Error message on browser console:浏览器控制台上的错误消息:

core.js:1671 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. core.js:1671 错误错误:未捕获(承诺):错误:无法匹配任何路由。 URL Segment: 'monitorpatient' URL 段:'monitorpatient'
Error: Cannot match any routes.错误:无法匹配任何路由。 URL Segment: 'monitorpatient' URL 段:'monitorpatient'

Note: I have just started Angular.注意:我刚刚开始使用 Angular。

I guess your item.route has the value of 'monitorpatient' but your actual route is 'main/monitorpatient'.我猜您的 item.route 的值为“monitorpatient”,但您的实际路线是“main/monitorpatient”。 You should be doing你应该做

 this.router.navigate(['/', 'main', item.route]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM