【问题标题】:angular 6 router outlet only showing the first componentangular 6 路由器插座仅显示第一个组件
【发布时间】:2019-01-05 00:18:34
【问题描述】:

我正在努力将 HTML5 模板转换为 Angular 6 SPA。 HTML5 模板有 7 个部分,我已将它们转换为 7 个组件:主页、关于、图库、服务、客户、推荐和定价。

在 app.component.html 我有:

<app-header></app-header>
<app-about></app-about>
<app-gallery></app-gallery>
<app-services></app-services> 
<app-testimonials></app-testimonials>     
<app-clients></app-clients>
<app-pricing></app-pricing>
<app-footer></app-footer>

SPA 显示正确(但仍不使用角度路由)。

当我将 app.component.html 中的上述代码替换为 &lt;router-outlet&gt;&lt;/router-outlet&gt; 时,我遇到的问题是只有 header.component.html 出现。

app-routing.module.ts如下:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '../../node_modules/@angular/router';

import { HeaderComponent } from './header/header.component';
import { AboutComponent } from './about/about.component';
import { GalleryComponent } from './gallery/gallery.component';
import { ServicesComponent } from './services/services.component';
import { ClientsComponent } from './clients/clients.component';
import { TestimonialsComponent } from './testimonials/testimonials.component';
import { PricingComponent } from './pricing/pricing.component';

const routes: Routes = [
  {path:'', redirectTo:'/home', pathMatch: 'full'},
  {path:'home', component: HeaderComponent},
  {path:'about', component: AboutComponent},
  {path:'gallery', component: GalleryComponent},
  {path:'services', component: ServicesComponent},
  {path:'clients', component: ClientsComponent},
  {path:'testimonials', component: TestimonialsComponent},
  {path:'pricing', component: PricingComponent}
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes),
    CommonModule
  ],
  declarations: [],
  exports: [RouterModule]
})
export class AppRoutingModule { }

我的问题是:为什么&lt;router-outlet&gt;&lt;/router-outlet&gt; 只显示标题组件而没有显示其他 6 个组件?。

我如何指定 Angular,我想在登录页面中显示哪些组件?。

【问题讨论】:

  • 您的每个组件都在不同的路线上,因此/about 会显示您的AboutComponent(尽管它不再显示HeaderComponent
  • 您是否将 AppRoutingModule 包含在您的导入中:[ ... ] in app.module.ts?
  • 您是否有菜单或其他东西可供用户用来选择他们想要查看的组件?应该采取什么行动来改变所显示的页面?
  • 是的,AppRoutingModule 在 app.module.ts 的导入中。
  • 是的,页面顶部有一个菜单,可以与其他页面一起滚动。

标签: angular html routing router-outlet


【解决方案1】:

当您使用router-outlet 并按照您的方式设置路由配置时,路由器将一次在该路由器插座中显示一个组件的模板。

如果您想一次显示所有组件,请将代码保留原样。你不需要路由。

如果您确实想一次显示一个组件,则需要某种机制来确定何时显示哪个组件的模板。

例如,在我的代码中,我有一个菜单和一个路由器插座:

<nav class='navbar navbar-expand navbar-light bg-light'>
    <a class='navbar-brand'>{{pageTitle}}</a>
    <ul class='nav nav-pills'>
      <li><a class='nav-link' [routerLink]="['/welcome']">Home</a></li>
      <li><a class='nav-link' [routerLink]="['/products']">Product List</a></li>
    </ul>
</nav>
<div class='container'>
  <router-outlet></router-outlet>
</div>

当用户选择一个菜单选项时,我会在路由器插座中显示该菜单选项的组件模板。在此示例中主页或产品列表。

【讨论】:

  • 我是 Angular 的新手,我正在尝试了解它是如何工作的。如果可能的话,我想使用角度路由。有什么方法可以指示角度以将多个组件显示为主页?就像单个路径的组件数组一样?
  • 没有。并不真地。但是,您可以定义具有多级路由器出口的路由层次结构。我在这里有一个屏幕截图:stackoverflow.com/questions/51501471/… 你也可以使用命名的插座定义“并排”路线,但是这些仍然存在很多问题,所以我不经常推荐它们。
  • 如果您有兴趣,我有一个 youtube 视频,介绍了这里的一些路由功能:youtube.com/watch?v=LaIAHOSKHCQ
猜你喜欢
  • 2018-12-15
  • 1970-01-01
  • 2018-11-24
  • 2019-01-05
  • 2016-12-30
  • 2019-02-07
  • 2018-11-14
  • 1970-01-01
  • 2017-03-09
相关资源
最近更新 更多