【问题标题】:Angular 2 - Child componente of another child component doesn't loadAngular 2 - 另一个子组件的子组件不加载
【发布时间】:2018-05-10 04:17:51
【问题描述】:

我配置了路由,但是在应用程序初始化时组件没有呈现。我需要在blog 组件中渲染posts 组件,因为博客已经正确加载。

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.routing.module';
import { BlogModule } from './blog/blog.module';
import { CandidatosModule } from './candidatos/candidatos.module';
import { AboutModule } from './about/about.module';
import { NavbarModule } from './navbar/navbar.module';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BlogModule,
    CandidatosModule,
    AboutModule,
    NavbarModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.html:

<app-navbar></app-navbar>
<!-- corpo da página -->
<router-outlet></router-outlet>

app.routing.module.ts:

const appRoutes: Routes = [
    { path: '', component: BlogComponent},
    { path: 'blog', component: BlogComponent},
    { path: 'candidatos', component: CandidatosComponent},
    { path: 'sobre', component: AboutComponent},
];

blog.component.html:

<div id="conteudo" class="container-fluid">
  <div class="row">
    <div class="col-10">
      hello, world!
      <router-outlet></router-outlet>
    </div>
    <!-- direita -->
    <div class="col-md-2">
      <app-aside></app-aside>
    </div>
  </div>
</div>

blog.routing.module.ts:

const blogRoutes: Routes = [
  { path: 'blog', component: BlogComponent, children: [
      { path: '', component: PostsComponent },
      { path: 'posts', component: PostsComponent },
      { path: 'news', component: NewsComponent },
      { path: 'estudos', component: EstudosComponent }
    ]
  }
];

打印演示在哪里加载帖子组件:1

【问题讨论】:

  • 让我们从你的 app.module.ts 开始
  • 一个 plnkr 会很有帮助,因为您无法在此处提供所有文件的所有内容,请尝试创建一个 plnkr 或提供 app.module.ts 及其应用程序路由模块文件
  • @AakashUniyal 我正试图把它放在 stackblitz 上
  • @vish213 我将 app.module.ts 代码添加到问题中。
  • 看不到 AppRoutingModule

标签: angular angular2-routing angular4-router


【解决方案1】:

本论坛portuguese version用户Eduardo Vargas回答问题:

app.routing.module.ts:

const appRoutes: Routes = [
    { path: '',   redirectTo: '/blog', pathMatch: 'full' }
    { path: 'candidatos', component: CandidatosComponent},
    { path: 'sobre', component: AboutComponent},
]; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 2016-08-21
    • 2017-05-09
    相关资源
    最近更新 更多