【发布时间】:2021-10-20 09:14:10
【问题描述】:
我正在使用 VueJs 为博客创建帖子页面。
我有这个作为模板:
<template>
...
<q-item-section>
<q-item-label class="text-subtitle1">
<strong>Danny Connell</strong>
<span class="text-grey-7">
@danny__connell
</span>
</q-item-label>
<q-item-label class="qweet-content text-body1">{{ get_post().content }}</q-item-label>
</q-item-section>
...
</template>
我正在尝试将帖子的内容、vue 的方法(查询 firebase)获取到模板中。
export default {
name: 'PagePost',
methods: {
get_post(){
var post_id = this.$route.query.id
var post = {
id: post_id,
}
post.content = "hi"
db.collection('posts').doc(post_id).get().then(snapshot => {
const document = snapshot.data()
post.content = document.content //lets say the content is "hello"
return post
},
}
我在模板"hi" 中看到,但不是数据库中的内容(“hello”),当我删除post.content = "hi" 行时,我在html 的模板中看不到任何内容。
据我了解,数据库检索数据库内容需要时间,因此在完成数据库查询之前会跳到下一行。
我尝试使用 async/await,但我仍然没有得到帖子的内容:
export default {
name: 'PagePost',
methods: {
async get_post(){
var post_id = this.$route.query.id
var post = {
id: post_id,
}
await db.collection('posts').doc(post_id).get().then(snapshot => {
const document = snapshot.data()
post.content = document.content //lets say the content is "hello"
return post
},
}
如何将 db 的输出获取到 html 的模板中?
【问题讨论】:
标签: vue.js vuejs2 async-await vue-component