我的项目文件夹结构如下所示
|-- 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" },
]
}现在,我试图在user-profile.components.html中呈现home-page.component.html,因为我尝试在home-page.component.ts中添加示例代码片段,如下所示
<p>this is header</p>
<router-outlet></router-outlet>
<p>this is footer</p>我的url类似于本地主机:4200/#/用户配置文件下面,但它只呈现在除内容之外的输出下面。
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 { }发布于 2018-11-10 05:58:45
问题在于如何深入嵌套path /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" }]
}
}
];编辑
从导入中删除HomeRoutingModule。
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(HomeRoutingModule)
],
declarations: [UserProfileComponent, HomePageComponent]
})
export class HomeModule { }发布于 2018-11-10 06:03:57
只需将home-routing.module.ts更改为:
export const HomeRoutingModule: Routes = [
{path : "" , component : HomePageComponent},
{
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: "", loadChildren: "./home/home.module#HomeModule"} ,你的路由器应该工作得很好,比如/会加载HomeComponent,user-profile会加载UserProfileComponent。
编辑
您的home-routing.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,
HomeRoutingModule,
],
declarations: [UserProfileComponent, HomePageComponent]
})
export class HomeModule { }从RouterModule.forChild(HomeRoutingModule)中删除imports
发布于 2018-11-10 06:21:40
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" }]
}
}
];https://stackoverflow.com/questions/53236327
复制相似问题