6.1组件间通讯
输入属性(父组件 → 子组件)
@Input()
stockCode:string;实战
子组件
<p>order子组件:{{stockCode}}</p>@Input()
stockCode: string;父组件
输出属性(子组件 → 父组件)
中间人模式
Last updated
@Input()
stockCode:string;<p>order子组件:{{stockCode}}</p>@Input()
stockCode: string;Last updated
<input (input)="onInput($event)">
<app-order [stockCode]="stock"></app-order>stock: string;
constructor() {
}
onInput(event: any) {
this.stock = event.target.value;
}@Output('priceChange')
priceChange: EventEmitter<number> = new EventEmitter();
@Output('自定义事件名称')
自定义事件名: EventEmitter<自定义事件的返回类型> = new EventEmitter();this.priceChange.emit(num);
this.自定义事件名.emit(发射实际值);<app-price-quote (priceChange)="onPriceChange($event)"></app-price-quote>
<app-price-quote (priceChange)="事件处理函数(自定义事件返回值)"></app-price-quote> @Output('priceChange')
priceChange: EventEmitter<number> = new EventEmitter();
constructor() {
setInterval(() => {
const num: number = Math.random();
this.priceChange.emit(num);
}, 1000);
}<app-price-quote (priceChange)="onPriceChange($event)"></app-price-quote>
<p>app组件:{{price}}</p> price: string;
constructor() {}
onPriceChange(price: number) {
this.price = price.toString();
}