- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将新的 Firebase Web SDK v9(测试版 8)与 NuxtJS2 一起使用。我有一个子组件,允许用户编辑他的帖子数据。这会使用新的帖子更改更新 Firestore 中的文档。但是,我的问题是……如何让我的父组件实时更新这些新数据?
让我解释一下:
我在应用程序中有以下示例组件结构:
<PostCardDetail>
<PostEdit />
</PostCardDetail>
PostCardDetail.vue
我正在执行数据获取的脚本部分:
import { doc, getDoc } from 'firebase/firestore'
import { mapGetters } from 'vuex'
import { db } from '~/plugins/firebase'
export default {
name: 'PostCardDetail',
data() {
return {
post: {}
}
},
async fetch() {
const docRef = doc(db, 'posts', this.$route.params.postId)
const docSnap = await getDoc(docRef)
this.post = docSnap.data()
},
mounted () {
// I believe I will need to mount the realtime listener here?
},
}
</script>
我正在执行数据编辑的 PostEdit.vue
脚本部分:
editPost() {
const postRef = doc(db, 'posts', this.post.id)
await updateDoc(postRef, {
...editedPost,
updatedAt: serverTimestamp()
})
console.log('updated!')
this.$store.dispatch('edit', false)
alert('Post successfully updated!')
}
我能够成功编辑数据,但是要查看最新的变化,我需要手动刷新浏览器。用户编辑帖子后,如何使用 firestore 获得自动实时更新?我假设此监听器将位于 PostCardDetail
?
我的尝试:
mounted() {
// eslint-disable-next-line no-unused-vars
const unsub = onSnapshot(
doc(db, 'posts', this.$route.params.postId),
(snapshot) => {
this.post = snapshot.data()
}
)
}
这看起来工作正常,但感觉有点笨拙。这是使用 NuxtJS 解决此问题的推荐方法吗?
最佳答案
查询看起来非常好,与 documentation 中提到的相似.
I assume this listener will be in the mounted() hook property of PostCardDetail
理想情况下,您可能希望在创建组件后设置监听器,我经常看到监听器是在 created()
created() {
const unsub = onSnapshot(
doc(db, 'posts', this.$route.params.postId),
(snapshot) => {
this.post = snapshot.data()
}
)
}
要在其他地方使用 unsub
,您可以根据您的用例在数据属性中设置它或将其传递给其他组件。
关于javascript - NuxtJS 和 Firebase Web SDK Beta v9 : How to add realtime listener to firestore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68847519/
整个分析的新手,我发现这非常困惑 我想在我的 nuxt ssr webapp 中使用 Google Analytics 4,但我对众多选项感到不知所措 Quick issue on nuxtjs/go
图像文件是否必须保存在nuxtProject的静态文件夹中?相反,它将使用 axios 保存在后端服务器中。抱歉,我无法提供示例代码,因为我对此一无所知。计划是将保存在静态文件夹中并将前端 url 发
我认为这方面有很多问题。但是没有正确答案。如何查看生产模式错误日志的详细信息? 最佳答案 Nuxt 在 nuxt.config.js 文件中提供了一个标志来启用生产调试 { debug: true
我正在使用我在下面添加的部署脚本部署我的 NuxtJS 应用程序。我使用了本教程中的一些说明:https://www.digitalocean.com/community/tutorials/how-
大家好! 我面临着一个我找不到解决方法的大问题。 简而言之 - 我希望能够生成以前未知长度的多级路由来创建过滤器。 更多详情有一个网站site.com 它有部分:site.com/news、site.
我将我应该在我的 Nuxt 应用程序中使用的模板的所有文件(css、js、图像等。)添加到 assets/theme 文件夹中。有 assets 文件夹结构: 在 assets/theme/sass/
好吧,我从 nuxt 开始,我有以下路线: /home /dashboard /login 我想保护 /dashboard,但仅限于使用 Cookie 中的 token 登录的用户。 然后我创建了一个
调用 $auth.loginWith('google') 后,我得到 http://localhost:3000/ru/user-registration#state=D7xooLsNNxNGfbvc
我有一个网站,比如说 example.com它与子域 blog.example.com 共享布局和少量组件所以我想为此创建通用的nuxtJS项目,以便nuxtJS优化路由和所有东西,我不想为此创建2个
基本上我需要将脚本添加到我的 index.html 的头部, 所以我试过的是...... 在我的 nuxt.config.js 中 head: { script: [ {
我在 Nuxt 项目中使用 vuetify 时遇到问题,我按照文档说明您只需将 @nuxtjs/vuetify 依赖项安装到您的项目中,并在 中添加一行nuxt.config.js 文件的 >buil
我正在使用 NuxtJs auth module处理我在州内的授权。对于身份验证,我写了一个有效的 express api。 我的 nuxt.config.js 中有以下配置: axios: {
我最近使用js的moment函数来vue。 但它不起作用。 这是说那个时刻没有定义。 我在这里寻找解决方案,但其中任何一个都不适合我。 如何在 nuxt 中使用时刻? import moment f
我是 Vue 和 NuxtJs 的新手。我正在尝试使用 NuxtJs 构建网络摄像头,但遇到了一些问题 Take Photo
我正在使用 NuxtJS 的 AsyncData 方法从 API 获取视频文件,然后作为文件对象返回到页面。 像这样: async AsyncData({$axios}){ var user_
无法使用 nuxt-community 的 Firebase 模块链接从 Firebase 实时数据库读取/检索数据 here 。已经在 nuxt.config.js 文件中添加了我的 firebas
我的 NuxtJS 应用程序中有一个接受查询参数的路由。我正在尝试实现一种允许用户更改查询参数并重新加载页面的逻辑。 我试过: // this does not work because I'm al
我的 NuxtJS 应用程序中有一个接受查询参数的路由。我正在尝试实现一种允许用户更改查询参数并重新加载页面的逻辑。 我试过: // this does not work because I'm al
在服务器渲染代理工作正常。请求将发送至 custom-server.com/v1/places。但在浏览器中请求将转到 current-domain.com/api/places 为什么它在浏览器中不
当我尝试在 nuxtjs 中获取一些数据时显示此错误 Cannot read property '_normalized' of undefined 这是我在 nuxtjs 中的 axios 请求:
我是一名优秀的程序员,十分优秀!