【问题标题】:How to route to a randomly generated firestore id in Vue?如何路由到Vue中随机生成的firestore id?
【发布时间】:2020-08-07 02:12:42
【问题描述】:

我有一个以 Firestore 作为后端的帖子应用...将路由推送到该类别中的参数,以在子集合中显示存储在其中的帖子。

当点击“创建新类别”按钮时,我会找到一种方法立即推送路线,而不必等待将类别添加到列表中......但我不知道该怎么做那是因为类别的id是随机生成的,不知道如何获取,有什么想法吗?

这是我的模板:

<input v-model="newCategory" type="text" placeholder="Category name"/>
<button @click="addCategory">Create new category</button>

<div v-for="(category, index) in categories" :key="index">
  <router-link :to="{name: 'Posts', params: {categoryId: category.id}}">
    <h3>{{ category.categoryName }}</h3>
  </router-link>
</div>

还有脚本:

data() {
  return {
    newCategory: null,
  };
},
methods: {
  addCategory() {
    this.$firestore.categories.add({
      categoryName: this.newCategory
    });
    this.newCategory = null;
  }
},
firestore() {
  return {
    categories: db.collection("categories")
  };
}

【问题讨论】:

    标签: javascript html vue.js google-cloud-firestore vue-router


    【解决方案1】:

    尝试在data 下初始化您的categories:[](我还没有看到它应用)

    然后将this.$firestore.categories 改为this.categories。数据绑定应该对连接到 Firestore 的数组的新推送/添加做出反应

    addTodo方法https://github.com/vuejs/vuefire/blob/master/packages/vuefire/examples/index.html的检查示例

    【讨论】:

    • 我不明白这一点,我的问题会保持不变......如果我仍然没有得到.add之后的this.$router.push({ name: "posts", params: { categoryId: category.id } });,我该怎么办? Firestore 创建到刚刚添加到类别数组中的那个类别?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    相关资源
    最近更新 更多