Rxjs 一旦可观测数据变量不为空,如何发射

Rxjs 一旦可观测数据变量不为空,如何发射,rxjs,nuxt.js,Rxjs,Nuxt.js,我是RxJS的新手,我正试图弄清楚当数据可用时如何观察数据。我正在使用Nuxt SSR,我正在从Firebase获取数据。初始的post值设置为null,一旦数据对象可用,它应该只运行一次head()函数。我得到这个类型错误 Cannot read property 'pipe' of null post$.pipe is not a function 如果我启动post:{},作为空对象,我会得到这个类型的错误 Cannot read property 'pipe' of null po

我是RxJS的新手,我正试图弄清楚当数据可用时如何观察数据。我正在使用Nuxt SSR,我正在从Firebase获取数据。初始的
post
值设置为
null
,一旦数据对象可用,它应该只运行一次
head()
函数。我得到这个类型错误

Cannot read property 'pipe' of null
post$.pipe is not a function
如果我启动
post:{},
作为空对象,我会得到这个类型的错误

Cannot read property 'pipe' of null
post$.pipe is not a function
如果我能得到一些帮助或指导,我将不胜感激

//页面\:post.vue
从“vuex”导入{mapState,mapActions}
从'rxjs/operators'导入{take}
导出默认值{
fetch(){
this.fetchPost()
},
计算:{
…mapState('posts',['post']),
},
方法:{
…映射操作('posts',['fetchPost']),
},
总目(){
const post$=this.post
post$.pipe(take(1)).subscribe((post)=>{
返回{
标题:this.post.title,
链接:[{rel:'canonical',href:this.post.canonical}],
元:[
{hid:'name',itemprop:'name',content:this.post.title},
{
hid:“描述”,
itemprop:'说明',
内容:this.post.content,
},
],
}
})
},
}
//store\posts.js
导出常量状态=()=>({
post:null,
})
导出常量突变={
设置柱(状态、有效载荷){
state.post=有效载荷
},
}
导出常量操作={
异步fetchPost({commit},key){
const doc=wait postsCollection.doc(key.get)()
如果(doc.exists)提交('setPost',doc.dat())
},

}
您需要订阅一个可观察的
。据我所知,在您的情况下,
this.post
不是可观察的
类型。
由于
this.post
是在某个时间点填充的,因此您需要
订阅
一个
可观察的
,当您说
this.post
时,该
应该会发出数据。为此,您可以使用一个


请参见此示例:

我已使用
主题更新了我的问题,但是,在设置此.post
之前提前返回的代码。你能帮我看看我是否做错了什么吗?你在哪里设置
这个的值。post
。它是否在
setPost(状态,有效载荷){state.post=payload}
?如果是这样,您必须
postSubject.next(有效负载)
在那里。我无法编辑您的代码块,因为我不知道如何运行它。否则我可能会改变这一点。在这里,我创建了一个示例。它当前有错误,因为在设置数据之前没有定义变量。请参见此。浏览内联注释