【问题标题】:Possible to accessing child components in app component through angular 4?可以通过角度4访问应用程序组件中的子组件吗?
【发布时间】: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


【解决方案1】:

在app-component.ts中导入你要添加的组件

import { Component } from '@angular/core';
import { PersonalInfoComponent } from './personal-info/personal-info.component'; 

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'app';
}

并在 app.component.html 中添加选择器

<app-personal-info></app-personal-info>

【讨论】:

  • 重复视图用您尝试过的内容更新问题
  • "" 我在 "app.component.html" 中添加了这两行
  • 如果我删除我的视图不会改变并且路由将失败。所有页面都将重复相同的视图。
  • 检查您的路线,您已为 PersonalInfoComponent 添加了两条路线,这就是您获得重复路线的原因,从路线中删除空路径
【解决方案2】:

好的,感谢您的编辑,它更清晰了。所以你是对的,你可以通过路由来实现这一点,并通过它创建一个页面,这就是你可以做到的:

  • 首先在 html 模板中注入您的主页中的所有组件,这似乎是 PersonalInfo: <html>...<div class="col-md-6"><app-skills-info-component [someInput]="someData"></app-skills-info-component> </div> <div class="col-md-3"> <app-other-info-component></app-other-info-component> </div>

然后只需在您的子组件中添加另一个 div col-md-12 以便在您仅查看组件时它会占用整个大小。你的路线看起来不错,我会保持这样。

编辑:确实在您的应用程序 html 中只需使用 &lt;app-personal info&gt;&lt;/app-personal-info&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 2020-06-10
    相关资源
    最近更新 更多