首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >路由器出口不呈现

路由器出口不呈现
EN

Stack Overflow用户
提问于 2018-11-10 05:45:58
回答 3查看 5.7K关注 0票数 2

我的项目文件夹结构如下所示

代码语言:javascript
复制
|-- 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如下所示

代码语言:javascript
复制
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如下所示

代码语言:javascript
复制
  { 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中添加示例代码片段,如下所示

代码语言:javascript
复制
<p>this is header</p>
<router-outlet></router-outlet>
<p>this is footer</p>

我的url类似于本地主机:4200/#/用户配置文件下面,但它只呈现在除内容之外的输出下面。

代码语言:javascript
复制
this is header

this is footer

我想我错过了什么,甚至连我都搞不懂。有谁能指出,这段代码出了什么问题。

编辑: home.module.ts

代码语言:javascript
复制
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 { }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-11-10 05:58:45

问题在于如何深入嵌套path /user-profile/user-profile。您应该只使用路径一次。

路由的修改版本

app-routing.module.ts

代码语言:javascript
复制
{ path: "home", component: LoginComponent },
{
    path: "user-profile",
    component: HomePageComponent,
    children: [
      { path: "", loadChildren: "./home/home.module#HomeModule" },
    ]
  }

home-routing.module.ts

代码语言:javascript
复制
export const HomeRoutingModule: Routes = [
    {

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

编辑

从导入中删除HomeRoutingModule

代码语言:javascript
复制
@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(HomeRoutingModule)
  ],
  declarations: [UserProfileComponent, HomePageComponent]
})
export class HomeModule { }
票数 0
EN

Stack Overflow用户

发布于 2018-11-10 06:03:57

只需将home-routing.module.ts更改为:

代码语言:javascript
复制
    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为:

代码语言:javascript
复制
  { path: "home", component: LoginComponent },
  { path: "", loadChildren: "./home/home.module#HomeModule"} ,

你的路由器应该工作得很好,比如/会加载HomeComponentuser-profile会加载UserProfileComponent

编辑

您的home-routing.module,ts中有一个错误:

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2018-11-10 06:21:40

app-routing.module.ts

代码语言:javascript
复制
    { path: "home", component: LoginComponent },
  {
    path: "user-profile",
    component: HomePageComponent,
    loadChildren: "./home/home.module#HomeModule"
  }

home-routing.module.ts

代码语言:javascript
复制
export const HomeRoutingModule: Routes = [
    {

                path: "",
                component:UserProfileComponent ,
                data: {
                    title: "user-profile",
                    urls: [{ title: "user-profile", url: "user-profile" }]
                }
    }
];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53236327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档