我正在尝试学习角2,并试图在父组件中设置一个变量,其中包含来自我的子组件的数据。基本上,我在父视图中有一个子标题,我希望根据加载的子程序来更改标题和一些HTML。
父组件:
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);
}
}儿童部分:
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:
<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>在我的生活中,我无法让这种方法发挥作用,也无法确定我哪里出了问题。我总是可以把子标题放在我的孩子中,但是我不喜欢重复这么多次代码的想法,这会使我的布局变得更难实现。
发布于 2017-11-15 07:11:50
最好的方法是使用服务和observable,以便每当子组件更改时,新数据将立即反映在父组件中。
在你的service.ts里
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
public ngOnInit() {
this.myService.changedComponentName('child1');
}儿童部分2
public ngOnInit() {
this.myService.changedComponentName('child2');
}以此类推。
现在,要在父组件中获取更改的组件名称,
亲本component.ts
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
});
}
}发布于 2017-11-14 17:44:17
你在用你的父母的方法吗?$event?
如果是,那么为什么要在父类方法中使用let childDetails ={}。相反,您应该使用this.childDetails={}
onChildLoaded($event){
this.childDetails ={ title: $event.childTitle, helperBar: $event.childHelperBar };
console.log (childDetails);
}和父html应该是
<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/>,那么您应该使用共享服务
https://stackoverflow.com/questions/47291959
复制相似问题