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

Mik*_*son 6 routing angular

我花了太多时间在这个看似简单的问题上.我有一条路线,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 { }
Run Code Online (Sandbox Code Playgroud)

我已经检查过<base href="/">index.html.但是,当我手动输入时http://localhost:4200/page2,我总是被重定向回主页(定义中app.component).

我重复了快速入门指南的时间和次数,但无法理解为什么.我的路线定义出了什么问题?


编辑:我使用全新的应用程序(via ng new test-app)和新page2组件(ng generate component page2)进行了测试.仍然是同样的问题.我现在更加困惑.

Rah*_*ngh 13

我想你错过了添加 <router-outlet></router-outlet>到你的App.component.html,

将其添加到要加载路径组件的app.component.html中

  • 这。不敢相信它是如此简单! (2认同)