首页 > 解决方案 > 路由器插座不渲染

问题描述

我的项目文件夹结构如下

|-- 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.ts 中添加示例代码片段的user-profile.components.html内部进行渲染,如下所示home-page.component.html

<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 { }

标签: angulartypescript

解决方案


只需将您的更改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


推荐阅读