Declar一个Subject
类型变量在你的服务,并通过调用.next()
推数据,然后赶上在想赶上你的数据的另一个类。
export class MyClass {//(有一个服务实例) result:new Subject();
constructor() {
this.result.next('fired first result');
}
returnMethod(){
//Does something 1
this.result.next('fired returnMethod result');
}
}
现在你可以赶上这里通过订阅的结果。
// normally this is a component if angular is in context
export class myComponent {
// normally @output being used in component
@Output() returnMethod : EventEmitter<any> = new EventEmitter();
constructor(private muClass: MyClass) { // this will inject the service
operation(){
//Does something 2
this.myclass.result.subscribe((res) => {
//Something else
});
// this is a output variable and can be emitted if there is a child component.
this.returnMethod.emit();
}
是的,这是一个组成部分,我错了,谢谢! – user3166722
是否有效?如果它的父亲孩子,那么你也可以使用@output变量,然后以不同的方式发出并捕获它 –