简体   繁体   English

找不到角度2中的链接路径

[英]Cannot find the link path in angular 2

Why is it that I cannot view the page when I type in " http://localhost:4200/values " and I get this error: "Error: Cannot match any routes. URL Segment: 'values'" 为什么当我输入“ http:// localhost:4200 / values ”时却无法查看页面,并且出现此错误:“错误:无法匹配任何路由。URL段:'values'”

This is how my code goes. 这就是我的代码的方式。

app.routing.ts 应用程序路由

export const AppRoutes: Routes = [
  {
    path: '',
    component: FullComponent,
    children: [
      {
        path: '',
        redirectTo: '/starter',
        pathMatch: 'full'
      },
      {
        path: '',
        loadChildren:
          './material-component/material.module#MaterialComponentsModule'
      },
      {
        path: 'starter',
        loadChildren: './starter/starter.module#StarterModule'
      },
      {
        path: 'component',
        loadChildren: './components/component.module#ComponentModule'
      }
    ]
  }
];

component.module.ts component.module.ts

@NgModule({
    imports: [
        CommonModule,
        RouterModule.forChild(ComponentRoutes),
        DemoMaterialModule
    ],
    declarations: [
        ValuesComponent,
        LoginComponent
    ]
})
export class ComponentModule {}

component.routing.ts component.routing.ts

export const ComponentRoutes: Routes = [
    // {
    //     path: '',
    //     component: ValuesComponent
    // },
    {
        path: 'values',
        component: ValuesComponent
    },
    {
        path: 'login',
        component: LoginComponent
    }
];

and this is how I call it from the link 这就是我从链接中称呼它的方式

const MENUITEMS = [
  { state: 'starter', name: 'Starter Page', type: 'link', icon: 'av_timer' },
  { state: 'button', type: 'link', name: 'Buttons', icon: 'crop_7_5' },
  { state: 'values', type: 'link', name: 'Values Page', icon: 'assistant' },
];

@Injectable()
export class MenuItems {
  getMenuitem(): Menu[] {
    return MENUITEMS;
  }
}

html for the links 链接的html

<mat-list-item appAccordionLink *ngFor="let menuitem of menuItems.getMenuitem()" routerLinkActive="selected" group="{{menuitem.state}}">
        <a class="" appAccordionToggle [routerLink]="['/', menuitem.state]" *ngIf="menuitem.type === 'link'">
            <mat-icon>{{ menuitem.icon }}</mat-icon> 
            <span>{{ menuitem.name }}</span> 
            <span fxFlex></span> 
            <span class="label label-{{ badge.type }}" *ngFor="let badge of menuitem.badge">{{ badge.value }}</span> 
        </a>


    </mat-list-item>

Can you please show me how to do this right? 您能告诉我如何做到这一点吗? Thank you so much for your help. 非常感谢你的帮助。

On localhost:4200/component you are loading ComponentModule. localhost:4200/component您正在加载ComponentModule。 Once that module is loaded it will open find the path: '' and will load the component associated with that path in this case ValuesComponent. 加载该模块后,它将打开并找到path: ''并在这种情况下加载与该路径关联的组件ValuesComponent。 you have the path: 'values' for ValuesComponent as well. 您也有path: 'values' ValuesComponent也有path: 'values'

So as per your structure, there are no routes with localhost:4200/values , if you need to open ValuesComponent you need to load localhost:4200/component or localhost:4200/component/values 因此,根据您的结构,不存在带有localhost:4200/values路由,如果您需要打开ValuesComponent,则需要加载localhost:4200/componentlocalhost:4200/component/values

Hope this helps. 希望这可以帮助。 All the best! 祝一切顺利!

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

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