Angular 角度2路径匹配路由不';行不通

Angular 角度2路径匹配路由不';行不通,angular,angular2-routing,Angular,Angular2 Routing,我试着通过考试 我有路由部分的问题 项目结构: 在编写的教程中,如果在模板中写入: app.component.html: <h1>{{title}}</h1> <nav> <a routerLink="/dashboard">Dashboard</a> <a routerLink="/heroes">Heroes</a> </nav> <router-outlet>&l

我试着通过考试

我有路由部分的问题

项目结构:

在编写的教程中,如果在模板中写入:

app.component.html:

<h1>{{title}}</h1>
<nav>
    <a routerLink="/dashboard">Dashboard</a>
    <a routerLink="/heroes">Heroes</a>
</nav>
<router-outlet></router-outlet>
import { NgModule }       from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { FormsModule }    from '@angular/forms';
import { RouterModule }   from '@angular/router';

import { AppComponent }        from './app.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroesComponent }     from './heroes.component';
import { HeroService }         from './hero.service';
import {DashBoardComponent} from "./dashboard.component";

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        RouterModule.forRoot([
            {
                path: 'heroes',
                component: HeroesComponent
            }, {
                path: 'dashboard',
                component: DashBoardComponent,
                pathMatch: 'full'
            }, {
                path: 'detail/:id',
                component: HeroDetailComponent
            },
        ])
    ],
    declarations: [
        AppComponent,
        HeroDetailComponent,
        HeroesComponent,
        DashBoardComponent
    ],
    providers: [
        HeroService
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
}
然后

要在浏览器中查看这些更改,请转到应用程序根目录(/) 并重新加载。该应用程序显示仪表板,我们可以在 仪表板和英雄

就我而言,我看到:

因此,重新加载页面后不会重定向到/dashboard

我错了什么

p.S.

请随时要求我为帖子添加更多详细信息。

记住,浏览器在
http://localhost:3000
npm启动时的url与路由配置中的任何url都不匹配,因此您需要在
RouterModule.forRoot
中添加以下重定向规则,以便应用程序在启动时显示
仪表板
启动后,您可以在浏览器地址栏中看到一个漂亮的URL,上面写着
/dashboard

{
  path: '',
  redirectTo: '/dashboard',
  pathMatch: 'full'
}

请记住,浏览器在
http://localhost:3000
npm start时的url,它与路由配置中的任何url都不匹配,因此需要在
RouterModule.forRoot中添加以下重定向规则,以便应用程序在启动时显示
仪表板
,您可以在浏览器中看到一个漂亮的url显示
/dashboard
的地址栏:

{
  path: '',
  redirectTo: '/dashboard',
  pathMatch: 'full'
}

你有没有检查控制台有没有错误?也许它找不到您的一个组件?您是否在app.module.ts中添加了RouterModule?@Jaime Torres console为空。如果单击链接-将打开每个组件properly@RumesShyaman,是的,我已经添加了整个路由代码?你可以添加整个路由代码吗?你检查控制台有没有错误?也许它找不到您的一个组件?您是否在app.module.ts中添加了RouterModule?@Jaime Torres console为空。如果单击链接-将打开每个组件properly@RumesShyaman,是的,我已经加上了你能加上整个路由代码吗?。