【相关教程推荐:《angular教程》】
1、通过输入型绑定把数据从父组件传到子组件
child.component.ts
export class childcomponent implements oninit { @input() hero: any; @input('master') mastername: string; // 第二个 @input 为子组件的属性名 mastername 指定一个别名 master constructor() { } ngoninit(): void { }}
child.component.html
<div style="background-color: #749f84"> <p>child works!</p> <h3>{{hero?.name}} says:</h3> <p>i, {{hero?.name}}, am at your service, {{mastername}}.</p></div>
parent.component.ts
export class parentcomponent implements oninit { hero = {name: 'qxj'} master = 'master' constructor() { } ngoninit(): void { }}
parent.component.html
<app-child [hero]="hero" [master]="master"></app-child>
2、父组件监听子组件的事件
child.component.ts
export class childcomponent implements oninit { @input() name: string; @output() voted = new eventemitter<boolean>(); didvote = false; vote(agreed: boolean) { this.voted.emit(agreed); this.didvote = true; } constructor() { } ngoninit(): void { }}
child.component.html
<h4>{{name}}</h4><button (click)="vote(true)" [disabled]="didvote">agree</button><button (click)="vote(false)" [disabled]="didvote">disagree</button>
parent.component.ts
export class parentcomponent implements oninit { agreed = 0 disagreed = 0 voters = ['narco', 'celeritas', 'bombasto'] onvoted(agreed: boolean) { agreed ? this.agreed++ : this.disagreed++ } constructor() { } ngoninit(): void { }}
parent.component.html
<h2>should mankind colonize the universe?</h2><h3>agree: {{agreed}}, disagree: {{disagreed}}</h3><app-child *ngfor="let voter of voters" [name]="voter" (voted)="onvoted($event)"></app-child>
3、父组件与子组件通过本地变量互动
父组件不能使用数据绑定来读取子组件的属性或调用子组件的方法。但可以在父组件模板里,新建一个本地变量来代表子组件,然后利用这个变量来读取子组件的属性和调用子组件的方法,如下例所示。
子组件 countdowntimercomponent 进行倒计时,归零时发射一个导弹。start 和 stop 方法负责控制时钟并在模板里显示倒计时的状态信息。
child.component.ts
export class childcomponent implements oninit, ondestroy { intervalid = 0 message = '' seconds = 11 cleartimer() { clearinterval(this.intervalid) } ngoninit() { this.start() } ngondestroy() { this.cleartimer() } start() { this.countdown() } stop() { this.cleartimer() this.message = `holding at t-${this.seconds} seconds` } private countdown() { this.cleartimer() this.intervalid = window.setinterval(() => { this.seconds -= 1 if (this.seconds === 0) { this.message = 'blast off!' } else { if (this.seconds < 0) { this.seconds = 10 } // reset this.message = `t-${this.seconds} seconds and counting` } }, 1000) }}
child.component.html
<p>{{message}}</p>
parent.component.ts
export class parentcomponent implements oninit { constructor() { } ngoninit(): void { }}
parent.component.html
<h3>countdown to liftoff (via local variable)</h3><button (click)="child.start()">start</button><button (click)="child.stop()">stop</button><div class="seconds">{{child.seconds}}</div><app-child #child></app-child>
4、父组件调用@viewchild()
这个本地变量方法是个简单便利的方法。但是它也有局限性,因为父组件-子组件的连接必须全部在父组件的模板中进行。父组件本身的代码对子组件没有访问权。
如果父组件的类需要读取子组件的属性值或调用子组件的方法,就不能使用本地变量方法。
当父组件类需要这种访问时,可以把子组件作为 viewchild,***注入***到父组件里面。
countdown-parent.component.ts
import {afterviewinit, component, viewchild} from '@angular/core'import {childcomponent} from '../child/child.component'@component({ selector: 'app-parent-vc', template: ` <h3>countdown to liftoff (via viewchild)</h3> <button (click)="start()">start</button> <button (click)="stop()">stop</button> <div class="seconds">{{ seconds() }}</div> <app-child></app-child> `,})export class countdownparentcomponent implements afterviewinit { @viewchild(childcomponent) private timercomponent: childcomponent seconds() { return 0 } ngafterviewinit() { // redefine `seconds()` to get from the `childcomponent.seconds` ... // but wait a tick first to avoid one-time devmode // unidirectional-data-flow-violation error settimeout(() => { this.seconds = () => this.timercomponent.seconds }, 0) } start() { this.timercomponent.start() } stop() { this.timercomponent.stop() }}
更多编程相关知识,请访问:编程入门!!
以上就是angular组件间怎么进行交互?常用交互方法介绍的详细内容。
