我使用从组件A到B的服务传递值
Servie A
public data: // is a object
public processData() {
// process about seconds and set value to data variable
}
组件B
private data; // is object
constructor(private servieA: ServieA) {}
ngOnInit() {
this.data = this.servieA.data;
}
但我的代码不起作用。请帮助我,非常感谢你。
答案 0 :(得分:1)
您可以使用rxjs提供的Subject Observable
公共服务
transactionConfig.connectionProvider()
组件A
import {Subject} from 'rxjs/Subject';
export class CommonService {
dataChanged = new Subject<any>();
}
组件B
export class AComponent implements onInit {
constructor(commonService: CommonService) {}
ngOnInit() {
//pass data after some activity
this.commonService.datachanged.next(somedata);
}
}