【问题标题】:Vuefire - get key within v-for loopVuefire - 在 v-for 循环中获取密钥
【发布时间】:2018-04-06 14:21:53
【问题描述】:

我是 Vue 和 Vuefire 的新手。我正在尝试使用密钥作为 URL ID 创建指向页面的动态链接。

我的参考设置为

let db = app.database();
const postsRef = db.ref('posts');

我正在将全局引用导入我的应用页面,就像这样 -

firebase: {
  posts: {
    source: postsRef,
    asObject: true,
    readyCallback: function () {
      console.log(postsRef)
    }
  },
},

并像这样遍历帖子对象 -

<div v-for="post in posts">
  <a :href="'post/' + post.key"></a>
</div>

我的数据库的结构是这样的 - DB Structure

考虑到我可能想在帖子页面上检索,最好不使用 URL 参数,使用密钥的最佳方法是什么?当我记录postsRef 对象时,我看到父值为null,而键值为posts。我希望键值在 for 循环中检索元素的唯一 ID。

【问题讨论】:

  • 我也在寻找这样的解决方案:/

标签: javascript firebase firebase-realtime-database vue.js vuefire


【解决方案1】:

所以基本上,删除 asObject: true 并将其作为数组导入反而对我有用。在我的情况下,密钥可以访问为

post['.key']

【讨论】:

    猜你喜欢
    • 2019-03-20
    • 2020-11-25
    • 1970-01-01
    • 2017-06-03
    • 2019-04-06
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    相关资源
    最近更新 更多