代码之家  ›  专栏  ›  技术社区  ›  Mike Henderson

角度路由始终重定向到主页

  •  13
  • Mike Henderson  · 技术社区  · 8 年前

    page2

    // app.module.ts
    import { Page2Component } from './page2/page2.component';
    
    @NgModule({
      declarations: [
        AppComponent,
        Page2Component
      ],
      imports: [
        BrowserModule, RouterModule.forRoot([
          { path: 'page2', component: Page2Component },
        ])
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    <base href="/"> 在里面 index.html . 但是,当我手动输入时 http://localhost:4200/page2 app.component

    我一遍又一遍地阅读《快速入门指南》,但不知道为什么。我的路线定义出了什么问题?


    ng new test-app )和新的 第2页 ng generate component page2 ). 还是一样的问题。我现在更困惑了。

    3 回复  |  直到 8 年前
        1
  •  24
  •   Rahul Singh    8 年前

    <router-outlet></router-outlet>

    将其添加到应用程序中。组成部分要在其中加载路由组件的html

        2
  •  2
  •   Greg Wozniak    8 年前

    Angular2 - app.module routing: blank path not redirecting to component

    export const ROUTES: Routes = [
      { path: '', component: LoginComponent },
      { path: 'dashboard', component: DashboardComponent  }
    ];
    

    export const ROUTES: Routes = [
      { path: 'dashboard', component: DashboardComponent  },
      { path: '', component: LoginComponent }
    ];
    

    它开始起作用了。

        3
  •  0
  •   Z. Bagley    8 年前

    确保包括:

    import { RouterModule, Routes } from '@angular/router';
    

    ng build this post ng serve 没有问题。

    推荐文章