未出现在道具中的方法的返回数据
我无法将数据从方法传递给道具。该方法从Firestore的集合中获取一个字段。
这是父母的组合:
<template>
<post-card
v-for="post in posts"
:key="post.id"
:userId="post.userId"
:userName="userName(post.userId)"
/>
</template>
<script>
components: {
PostCard
}
methods: {
userName(id){
this.$fire.firestore.collection('users').doc(id).get().then(snapshot => {
console.log("id", snapshot.data())
return snapshot.data().name
})
}
}
</script>
这是明信片的组合:
<template>
{{userName}}
</template>
<script>
name: 'post-card'.
props: [
'userName',
'userId'
],
</script>
道具可以通过这种方式获取数据吗?一旦工作,这会是反应性的吗?
I can't pass data from a method to a prop. The method gets a field from a collection in firestore.
here is the parent compenent:
<template>
<post-card
v-for="post in posts"
:key="post.id"
:userId="post.userId"
:userName="userName(post.userId)"
/>
</template>
<script>
components: {
PostCard
}
methods: {
userName(id){
this.$fire.firestore.collection('users').doc(id).get().then(snapshot => {
console.log("id", snapshot.data())
return snapshot.data().name
})
}
}
</script>
and here is the post card compenent:
<template>
{{userName}}
</template>
<script>
name: 'post-card'.
props: [
'userName',
'userId'
],
</script>
can a prop get data this way? will this be reactive once working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法访问您的特定Firestore数据,并且嘲笑它非常困难,因此我使用Jsonplaceholder来示例。
这是我将如何处理此类情况
index.vue
pcardc.vue
在这里,我在这里使用
this。基本上是一个花哨的
axios + .data
组合,没什么特别的。我没有使用
:用户名=“ username(post.userid)”
在您时代,因为这本身不是可行的。在这种情况下,模板是同步的,这意味着在运行功能时,您将无法获得post.userid
,因为请求仍在待处理。同时,您的孩子需要此信息才能使自己呈现。在Vue中,通常您会在 Child 组件本身或
安装
挂钩中进行此类逻辑。这主要是因为vue子组件如何安装在此处所述: a>您可能可以使用这种挂载的技巧,但我不建议以这种方式工作,因为它将是hacky hacky hacky ,总体而言,更加困难和不必要。
I don't have access to your specific Firestore data and mocking it would be quite difficult so I used JSONplaceholder for my example.
Here is how I would handle such case
index.vue
PostCard.vue
Here, I'm using
this.$axios.$get
but it's basically a fancyaxios + .data
combo, nothing special.I didn't used
:userName="userName(post.userId)"
in the parent as you because this is not feasible per se. A template is synchronous in that case, meaning that you will not getpost.userId
when running your function because the request would still be pending. Meanwhile, your child needs this info straight to render himself.In Vue, you usually do this kind of logic inside of the child component itself or in a
mounted
hook in pair with the collection fetch. This is mainly because of how a Vue child component mounts as explained here: https://stackoverflow.com/a/44319825/8816585You could maybe use this kind of mounted trick but I don't recommend working in that way because it will be hacky, overall more difficult and unnecessary.