網站搜尋

angular - 路由出口沒有渲染


我的專案資料夾結構如下

|-- app
     |-- home
       |-- home-page
           |--home-page.component.css
           |--home-page.component.html
           |--home-page.component.spec.ts
           |--home-page.component.ts
       |-- login
           |--home-page.component.css
           |--home-page.component.html
           |--home-page.component.spec.ts
           |--home-page.component.ts
       |-- user-profile
           |--user-profile.component.css
           |--user-profile.component.html
           |--user-profile.component.spec.ts
           |--user-profile.component.ts
     |-- home-routing.module.ts
     |-- home.module.spec.ts
     |-- home.module.ts
|-- app-routing.module.ts
|-- app.component.css
|-- app.component.html
|-- app.component.ts
|-- app.module.ts

我的 home-routing.module.ts 組成如下

export const HomeRoutingModule: Routes = [
    {
        path: "user-profile",
        children: [
            {
                path: "user-profile",
                component:UserProfileComponent ,
                data: {
                    title: "user-profile",
                    urls: [{ title: "user-profile", url: "user-profile" }]
                }
            }
        ]
    }
];

而 app-routing.module.ts 如下所示

  { path: "home", component: LoginComponent },
  {
    path: "user-profile",
    component: HomePageComponent,
    children: [
      { path: "user-profile", loadChildren: "./home/home.module#HomeModule" },
    ]
  }

現在我正在嘗試在 home-page.component.html 中渲染 user-profile.components.html,為此我嘗試在 home-page.component.ts 中添加如下範例程式碼片段

<p>this is header</p>
<router-outlet></router-outlet>
<p>this is footer</p>

我的 URL 如下 localhost:4200/#/user-profile,但它除了內容外只呈現以下輸出

this is header

this is footer

我覺得我漏掉了一些東西,即使我自己也無法弄清楚。有人能指出這段代碼出了什麼問題嗎?

編輯: home.module.ts

import { NgModule } from '@angular/core';
import { CommonModule, } from '@angular/common';
import { RouterModule } from '@angular/router';

import { HomeRoutingModule } from './home-routing.module';
import { LoginComponent } from './login/login.component';
import { UserProfileComponent } from './user-profile/user-profile.component';
import { HomePageComponent } from './home-page/home-page.component';


@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(HomeRoutingModule),
    HomeRoutingModule,
  ],
  declarations: [UserProfileComponent, HomePageComponent]
})
export class HomeModule { }

答案#1

問題在於你的路徑 /user-profile/user-profile 的深層嵌套。你應該只使用一次該路徑。

您路由的修改版本

app-routing.module.ts

{ path: "home", component: LoginComponent },
{
    path: "user-profile",
    component: HomePageComponent,
    children: [
      { path: "", loadChildren: "./home/home.module#HomeModule" },
    ]
  }

home-routing.module.ts

export const HomeRoutingModule: Routes = [
    {

                path: "",
                component:UserProfileComponent ,
                data: {
                    title: "user-profile",
                    urls: [{ title: "user-profile", url: "user-profile" }]
                }
    }
];

編輯

從 imports 中移除 HomeRoutingModule。

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(HomeRoutingModule)
  ],
  declarations: [UserProfileComponent, HomePageComponent]
})
export class HomeModule { }

答案#2

app-routing.module.ts

    { path: "home", component: LoginComponent },
  {
    path: "user-profile",
    component: HomePageComponent,
    loadChildren: "./home/home.module#HomeModule"
  }

home-routing.module.ts

export const HomeRoutingModule: Routes = [
    {

                path: "",
                component:UserProfileComponent ,
                data: {
                    title: "user-profile",
                    urls: [{ title: "user-profile", url: "user-profile" }]
                }
    }
];