【发布时间】:2021-03-26 23:02:03
【问题描述】:
这是共享服务 (dShareService) =>
@Injectable()
export class DInfoShareService {
constructor() { }
// Observable sources
private dInfo = new Subject<DInfo>();
dInfo$ = this.dInfo.asObservable();
// Service message commands
public SetDInfo(dinfo: DInfo) {
this.dInfo.next(dinfo);
}
}
这是父母 =>
在父级,我有按钮单击事件并将数据传递给方法,值是传递给服务。
GoToDetail(value){
this.dShareService.SetDInfo(value);
//this is child component and call by route, basically, I use this state and pass data to child
//component but this time, I have 3 tab page at UI and each page needs this data.
this.router.navigateByUrl('/dchild', {
state: {dInfo: value}
});
这是子组件 =>
在构造函数中,
this.dinfoShareService.dInfo$.subscribe(res=>
{
//this one never happen
this.dInfo = res;
}
);
这个在子节点上的订阅永远不会被触发。我可以知道我错了吗?
【问题讨论】:
-
使用 BehaviorSubject 代替
-
@enno.void 谢谢。我更改为 BehaviourSubject 并按预期工作。
标签: angular typescript angular-components