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>
我的网址如下 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 的深层嵌套上。你应该只使用一次该路径。
你路由的修改版本
应用路由模块.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" }]
}
}
];
