【发布时间】: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 中的上述代码替换为 <router-outlet></router-outlet> 时,我遇到的问题是只有 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 { }
我的问题是:为什么<router-outlet></router-outlet> 只显示标题组件而没有显示其他 6 个组件?。
我如何指定 Angular,我想在登录页面中显示哪些组件?。
【问题讨论】:
-
您的每个组件都在不同的路线上,因此
/about会显示您的AboutComponent(尽管它不再显示HeaderComponent) -
您是否将 AppRoutingModule 包含在您的导入中:[ ... ] in app.module.ts?
-
您是否有菜单或其他东西可供用户用来选择他们想要查看的组件?应该采取什么行动来改变所显示的页面?
-
是的,AppRoutingModule 在 app.module.ts 的导入中。
-
是的,页面顶部有一个菜单,可以与其他页面一起滚动。
标签: angular html routing router-outlet