Java里面子级与父级的作用,如何从父级到子级以角度发出事件?

据我所知,有两种标准方法可以做到这一点 .

1. @Input

只要父项中的数据发生更改,子项就会在ngOnChanges方法中得到通知 . 孩子可以采取行动 . 这是与孩子互动的标准方式 .

Parent-Component

public inputToChild: Object;

Parent-HTML

Child-Component: @Input() data;

ngOnChanges(changes: SimpleChanges){

// Whenever the data in the parent changes, this method gets triggered. You

can act on the changes here. You will have both the previous value and the

current value here.

}

Shared service concept

在共享服务中创建服务并使用observable . 孩子订阅它,每当有变化时,孩子都会收到通知 . 这也是一种流行的方法 . 如果要发送除传递的数据以外的其他内容作为输入,可以使用此功能 .

SharedService

subject: Subject;

Parent-Component

constructor(sharedService: SharedService)

this.sharedService.subject.next(data);

Child-Component

constructor(sharedService: SharedService)

this.sharedService.subject.subscribe((data)=>{

// Whenever the parent emits using the next method, you can receive the data

in here and act on it.})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值