简体   繁体   English

Angular 2教程:英雄大厅的路由器编译但不在浏览器中显示内容

[英]Angular 2 Tutorial: Routers for Hall of Heroes compiles but not displaying content in browser

I am trying to work through the Angular 2 tutorial and am getting stuck on the "Router" section. 我正在尝试通过Angular 2教程,并陷入“路由器”部分。 Specifically the RouterModule in app.module.ts. 特别是app.module.ts中的RouterModule。

As soon as I add: 我一添加:

    RouterModule.forRoot([
        {
            path: '',
            redirectTo: '/heroes',
            pathMatch: 'full'
        },
        {
            path: 'heroes',
            component: HeroesComponent
        }

to the imports array, the content in the browser is no longer displayed. 对于imports数组,不再显示浏览器中的内容。 Even after I add the routes to app.component.ts file as shown below the application only displays "Loading AppComponent content here ..." even though all files compile. 即使我将路由添加到app.component.ts文件后,如下所示,即使所有文件都已编译,应用程序也只显示“在此处加载AppComponent内容...”。

This effectively stops me in my tracks and cannot continue. 这实际上阻止了我的追踪,无法继续。 It was going fine until I add this point which means my code base is okay except the router stuff. 它一直很好,直到我添加这一点,这意味着我的代码库是好的,除了路由器的东西。

Is there an issue in the tutorial, perhaps something out of date? 教程中是否存在问题,可能是过时了?

Here is the code for app.module.ts: 这是app.module.ts的代码:

    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 { DashboardComponent } from './dashboard.component';

    import { HeroService } from './hero.service';

    @NgModule({
        imports: [
        BrowserModule,
        FormsModule,
        RouterModule.forRoot([
            {
                path: '',
                redirectTo: '/heroes',
                pathMatch: 'full'
            },
            {
                path: 'heroes',
                component: HeroesComponent
            }
        ])
      ],
      declarations: [
        AppComponent,
        HeroDetailComponent,
        HeroesComponent
      ],
      providers: [
        HeroService
      ],
      bootstrap: [
        AppComponent
      ]
    })

    export class AppModule { }

Here is the code from the app.component.ts: 以下是app.component.ts中的代码:

    import { Component } from '@angular/core';

    import { HeroesComponent } from './heroes.component';

    @Component({
        selector: 'my-app',
        template: `
            <h1>{{title}}</h1>
            <nav>
                <a routerLink="/heroes">Heroes</a>
            </nav>
            <router-outlet></router-outlet>
        `
    })

    export class AppComponent  {
        title = 'Tour of Heroes';
    }

Any help is greatly appreciated! 任何帮助是极大的赞赏!

I ran into a similar problem when I was doing the tutorials. 我在做教程时遇到了类似的问题。 I was able to resolve this by putting the following HTML base tag in the index.html. 我能够通过在index.html中放入以下HTML基本标记来解决这个问题。 Hope this helps! 希望这可以帮助!

<base href="/">

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

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