vue.js / Vue getter未定义错误

时间:2017-10-23 08:00:01

标签: vue.js vuex computed-properties

我的App.vue组件出错,getter:已记录

<li id="shoppinglists" v-if="!logged">...

ERROR LOG: '[Vue warn]: Property or method "logged" is not defined on the instance but referenced during render. 
Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. 

我不明白为什么它被声明为非定义,因为我没有得到另一个定义的getter的错误: currentUserId

<li v-else id="shoppinglists"><router-link :to="{ name: 'ShoppingLists', params: { id: currentUserId } }" >Shopping Lists</router-link></li>

Bothe定义为计算道具:

<script>
import store from '@/vuex/store'
import { mapGetters } from 'vuex'

export default {
  name: 'app',
  computed: {
    ...mapGetters([
      { currentUserId: 'getCurrentUserId' },
      { logged: 'getCurrentUserStatus' }
    ])
  },
  store
}
</script>

我的vex / getters.js是:

vuex / getters.js

export default {
  getCurrentUserStatus: state => state.logged,
  getCurrentUserId: state => state.currentUserId,
  ...
}

我的商店是

vuex / store.js

import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
...

Vue.use(Vuex)

const state = {
  logged: false,
  currentUserId: '',
  ...
}

export default new Vuex.Store({
  state,
  getters,
   ...
})

1 个答案:

答案 0 :(得分:0)

如果要使用具有不同名称的getter

,只需将对象传递给...mapGetters即可

所以语法是:

...mapGetters({
      currentUserId: 'getCurrentUserId',
      logged: 'getCurrentUserStatus' 
 })