首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@输出子程序与父级之间的通信。角2(5)

@输出子程序与父级之间的通信。角2(5)
EN

Stack Overflow用户
提问于 2017-11-14 17:34:19
回答 2查看 558关注 0票数 0

我正在尝试学习角2,并试图在父组件中设置一个变量,其中包含来自我的子组件的数据。基本上,我在父视图中有一个子标题,我希望根据加载的子程序来更改标题和一些HTML。

父组件:

代码语言:javascript
复制
import { Component, OnInit, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class ParentComponent {
  childDetails: {title: String, helperBar: String};

  onChildLoaded(childData: {
    childTitle: string,
    childHelperBar: string
  }) {
    this.childDetails ={
      title: childData.childTitle,
      helperBar: childData.childHelperBar
    };
    console.log (childDetails);
  }
}

儿童部分:

代码语言:javascript
复制
import { Component, OnInit, ViewEncapsulation, Output, EventEmitter } 
from '@angular/core';

@Component({
  selector: 'app-first-child',
  templateUrl: './first-child.component.html',
  styleUrls: ['./first-child.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class FirstChildComponent implements OnInit {
  @Output() childLoaded = new EventEmitter<{childTitle: string, 
childHelperBar: string}>();

  constructor() { }

  ngOnInit() {
    this.childLoaded.emit({
      childTitle: 'Dashboard',
      childHelperBar: '<div class="test"><button>Dash Button</button>
</div>'
    });
  }

}

最后,我的父HTML:

代码语言:javascript
复制
<div class="subheader">
  <h1>{{ childDetails.title }}</h1>
  {{ childDetails.helperBar }}
</div>
<nav class="sidebar">

</nav>

<main role="main" class="">
  <router-outlet (childLoaded)="onChildLoaded($event)"></router-outlet>
</main>

在我的生活中,我无法让这种方法发挥作用,也无法确定我哪里出了问题。我总是可以把子标题放在我的孩子中,但是我不喜欢重复这么多次代码的想法,这会使我的布局变得更难实现。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-15 07:11:50

最好的方法是使用服务和observable,以便每当子组件更改时,新数据将立即反映在父组件中。

在你的service.ts里

代码语言:javascript
复制
import { Subject } from 'rxjs/Subject'
import { Observable } from 'rxjs/Rx';

@Injectable()
export class myService {

  private pageName = new Subject<string>();

  pageNameChange$ = this.pageName.asObservable();
  changedComponentName(option:string){
        this.pageName.next(option);
  }
}

在你的孩子身上,

儿童部分1

代码语言:javascript
复制
public ngOnInit() {
  this.myService.changedComponentName('child1');
}

儿童部分2

代码语言:javascript
复制
public ngOnInit() {
   this.myService.changedComponentName('child2');
}

以此类推。

现在,要在父组件中获取更改的组件名称,

亲本component.ts

代码语言:javascript
复制
import { Subscription } from 'rxjs';

export class parentComponent {

    private subscription:Subscription;

    constructor(private myService:myService){

        this.myService.pageNameChange$.subscribe( /* get called on every child page changes*/
         newPageName => {

        console.log(newPageName); // you get your passed component name ( child1 / child2 /etc.. here in the 'newPageName' variable
    });
  }
}
票数 1
EN

Stack Overflow用户

发布于 2017-11-14 17:44:17

你在用你的父母的方法吗?$event

如果是,那么为什么要在父类方法中使用let childDetails ={}。相反,您应该使用this.childDetails={}

代码语言:javascript
复制
onChildLoaded($event){
 this.childDetails ={ title: $event.childTitle, helperBar: $event.childHelperBar }; 
console.log (childDetails); 
}

和父html应该是

代码语言:javascript
复制
<div class="subheader"> <h1>{{ childDetails.title }}</h1> {{ childDetails.helperBar }} </div> 
<nav class="sidebar"> </nav> 
<main role="main" class="">
 <app-first-child (childLoaded)="onChildLoaded($event)"></app-first-child> </main>

如果您不想使用<app-first-child/>,那么您应该使用共享服务

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47291959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档