我有一个这样的Vue组件:
<script>
import { mapActions, mapGetters } from 'vuex'
export default {
props: ['index'],
computed: {
...mapGetters([
'type',
'width',
'height',
'description',
'smtTagMeasureUnits',
'tagValue'
])
}
</script>
<template>
<div :class='["block","module-"+type(index), "width"+width(index), "height"+height(index)]'>
<h3>{{ description(index) }}</h3>
<div class="data">
<h1>{{ tagValue(index) }}</h1>
<h2 class="measure">{{ smtTagMeasureUnits(index) }}</h2>
</div>
</div>
</template>
<style>
...
</style>
Run Code Online (Sandbox Code Playgroud)
参数index(作为prop进入组件)已成功传递给getter:
getters: {
...
type: (state, getters) => (par) => {
return getters.widgetsConfig[par].type
},
width: (state, getters) => (par) => {
if (getters.widgetsConfig[par].width) {
return getters.widgetsConfig[par].width
} …
Run Code Online (Sandbox Code Playgroud) 我有一个带有多个路由和 vuex 的 vue-cli-4 应用程序。Firestore 数据库已成功连接 - 我的应用程序立即反映了从 Firestore 控制台应用于数据库的修改。
在离开包含与 Firestore“同步”的组件然后返回的路由后,开始发生奇怪的行为。在这种情况下,在 Firestore 控制台中修改数据后,onSnapshot()方法会触发多次。
我想知道每次离开路线后我是否应该以某种方式“手动”从 Firestore 取消订阅我的组件 - 可能是在destroy()钩子上。
代码的一部分:我同步PieChart.vue通过发射与公司的FireStore组件(位于/派路线)getDataPie行动()创建挂钩。
<script>
import { mapActions, mapGetters } from 'vuex'
export default {
name: 'PieChart',
created () {
this.getDataPie()
},
methods: {
...mapActions([
...
'getDataPie',
...
])
}
}
</script>
<template>
...
</template>
Run Code Online (Sandbox Code Playgroud)
在 vuex 模块中:
import { getDataPie } from '../../helpers/helpersPie.js'
...
actions: {
...
getDataPie: async ({ commit, getters }) …
Run Code Online (Sandbox Code Playgroud) 这里声明$fireStore可以在vuex action中访问:
async randomVuexAction({ commit, state, rootState }, userId) {
const ref = this.$fireStore.collection('users').doc(userId)
...
}
Run Code Online (Sandbox Code Playgroud)
问题:在 中触发操作后store/index.js
:
addItem: ({ commit, getters }, itemName) => {
const item = { name: itemName }
this.$fireStore.collection('items').add(item).then((res) => {})
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:Cannot read property '$fireStore' of undefined
。一般来说,console.log(this)
除了nuxtServerInit()
- 之外的所有操作中都会给出undefined
。那么是否有可能使用$fireStore
或者vuex
文档具有误导性?