Angular 2 - 另一个子组件的子组件不加载

时间:2018-05-10 04:17:51

标签: angular angular2-routing angular4-router

我配置了路由,但是在初始化应用程序时组件不会呈现。我需要=组件中的posts组件呈现,因为博客已正确加载。

app.module.ts

blog

app.component.html:

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.routing.module.ts:

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

blog.component.html:

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

blog.routing.module.ts:

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

打印演示加载帖子组件的位置:1

1 个答案:

答案 0 :(得分:0)

此论坛的Eduardo Vargas中的用户portuguese version回答了以下问题:

<强> app.routing.module.ts:

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