【发布时间】:2017-11-19 11:46:30
【问题描述】:
app-component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'app';
}
profile-info.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
@Component({
selector: 'app-personal-info',
templateUrl: './personal-info.component.html',
styleUrls: ['./personal-info.component.scss']
})
export class PersonalInfoComponent implements OnInit {
constructor(private route: ActivatedRoute, private router: Router) {
this.route.params.subscribe(res => console.log(''));
}
ngOnInit() {
}
}
这里我希望 profile-info 组件需要与 app 组件嵌套。
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { PersonalInfoComponent } from './personal-info/personal-info.component'; // Add this
import { WorkInfoComponent } from './work-info/work-info.component'; // Add this
import { SkillsInfoComponent } from './skills-info/skills-info.component'; // Add this
import { ProjectInfoComponent } from './project-info/project-info.component'; // Add this
import { EducationalInfoComponent } from './educational-info/educational-info.component'; // Add this
import { DeclarationInfoComponent } from './declaration-info/declaration-info.component'; // Add this
const routes: Routes = [
{
path: '',
component: PersonalInfoComponent
},
{
path: 'profile',
component: PersonalInfoComponent
},
{
path: 'work',
component: WorkInfoComponent
},
{
path: 'skills',
component: SkillsInfoComponent
},
{
path: 'project',
component: ProjectInfoComponent
},
{
path: 'education',
component: EducationalInfoComponent
},
{
path: 'declaration',
component: DeclarationInfoComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.components.html
<router-outlet></router-outlet>
<app-personal-info></app-personal-info>
预期
当我尝试打开 "localhost:4200" 时,所有组件 html 都会通过 app 组件显示在页面中。
当我尝试打开 "localhost:4200/profile" 时,只会显示 "profile-info.componet" html 视图。现在工作了。
【问题讨论】:
-
你有什么问题?
-
在角度 4 中与另一个组件共享一个组件
-
只需将组件的选择器添加到其他组件中
-
我想你的意思是在组件之间交换数据?还是使用 router-link 导航?
-
@Westi-Tech 参考上面的截图....希望其他组件的html模板也需要显示在应用组件中
标签: angular typescript components