未出现在道具中的方法的返回数据

发布于 2025-02-11 02:40:01 字数 891 浏览 2 评论 0原文

我无法将数据从方法传递给道具。该方法从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま昔日黯然 2025-02-18 02:40:01

我无法访问您的特定Firestore数据,并且嘲笑它非常困难,因此我使用Jsonplaceholder来示例。

这是我将如何处理此类情况

index.vue

<template>
  <div>
    <post-card v-for="user in users" :key="user.id" :user="user" />
  </div>
</template>

<script>
export default {
  components: {
    'post-card': () => import('~/components/PostCard.vue'),
  },
  data() {
    return {
      users: [],
    }
  },
  async mounted() {
    this.users = await this.$axios.$get(
      'https://jsonplaceholder.typicode.com/users/'
    )
  },
  // methods: {
  //   async fetchUsername(id) {
  //     const snapshot = await this.$fire.firestore
  //       .collection('users')
  //       .doc(id)
  //       .get()
  //     console.log('id', snapshot.data())
  //     return snapshot.data().name
  //   },
  // },
}
</script>

pcardc.vue

<template>
  <div>
    <span>{{ user.name }}</span>
    <pre>{{ details }}</pre>
    <hr />
  </div>
</template>

<script>
export default {
  name: 'PostCard',
  props: {
    user: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      details: {},
    }
  },
  async mounted() {
    this.details = await this.$axios.$get(
      `https://jsonplaceholder.typicode.com/users/${this.user.id}`
    )
  },
}
</script>

在这里,我在这里使用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

<template>
  <div>
    <post-card v-for="user in users" :key="user.id" :user="user" />
  </div>
</template>

<script>
export default {
  components: {
    'post-card': () => import('~/components/PostCard.vue'),
  },
  data() {
    return {
      users: [],
    }
  },
  async mounted() {
    this.users = await this.$axios.$get(
      'https://jsonplaceholder.typicode.com/users/'
    )
  },
  // methods: {
  //   async fetchUsername(id) {
  //     const snapshot = await this.$fire.firestore
  //       .collection('users')
  //       .doc(id)
  //       .get()
  //     console.log('id', snapshot.data())
  //     return snapshot.data().name
  //   },
  // },
}
</script>

PostCard.vue

<template>
  <div>
    <span>{{ user.name }}</span>
    <pre>{{ details }}</pre>
    <hr />
  </div>
</template>

<script>
export default {
  name: 'PostCard',
  props: {
    user: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      details: {},
    }
  },
  async mounted() {
    this.details = await this.$axios.$get(
      `https://jsonplaceholder.typicode.com/users/${this.user.id}`
    )
  },
}
</script>

Here, I'm using this.$axios.$get but it's basically a fancy axios + .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 get post.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/8816585

You 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文