可以通过@Input将数据从父对象发送到子对象,也可以通过@Output从子对象调用父对象上的方法,但我希望完全相反,也就是从父对象调用子对象上的方法.基本上是这样的:
@Component({
selector: 'parent',
directives: [Child],
template: `
<child
[fn]="parentFn"
></child>
`
})
class Parent {
constructor() {
this.parentFn()
}
parentFn() {
console.log('Parent triggering')
}
}
子元素:
@Component({
selector: 'child',
template: `...`
})
class Child {
@Input()
fn() {
console.log('triggered from the parent')
}
constructor() {}
}
背景是一种"获取"请求,即从子元素那里获取最新状态.
现在我知道我可以通过服务和主题/观察来实现这一点,但我想知道是否还有更直接的东西?