问题描述
我已经在服务类中创建了一个行为主题.
I have created a behaviour subject in a service class.
public personObject: BehaviorSubject<any> = new BehaviorSubject<any>({
personId: 1,
name: 'john doe'
});
在导入此服务的组件上,我已订阅了以下行为主题:
On a component that imports this service, i have subscribed this behavior subject like this:
` this._subscription.add(
this._bankService.personObject.subscribe(data => {
this.personObject = data;
console.log(data);
})
);`
但是我无法获得有关行为主体的确切数据集.请帮忙.
But I am not able to get exact data set in behavior subject. Please help.
修改我忘了提到我使用ViewContainerRef来创建同级组件.因此,我添加了一个对我的问题的评论很少的答案.
EditI forgot to mention that i have used ViewContainerRef to create my sibling comnponent. So i have added a answer with few comments on my issue.
推荐答案
我忘了提到我正在使用ViewContainerRef创建同级组件,结果发现行为主体与使用创建的组件的工作方式不同ViewContainerRef.
I forgot to mention that I was using I was using ViewContainerRef to create a sibling component and it turns out behavior subject does not work the same way with component created using ViewContainerRef.
任何对象的其他明智行为主体的工作方式与数字或字符串完全相同.我现在使用@Input将数据发送到组件.
Other wise Behaviour subjects of any object work exactly like with number or string. I used @Input to send data to component for now.
这篇关于如何为对象创建行为主体并在另一个组件上订阅它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!