NuxtJs Vuex |当我尝试使用... mapGetters或... mapActions时出现意外令牌

时间:2020-09-30 02:11:31

标签: javascript vue.js vuex nuxt.js

我是NuxtJs的新手。 当我尝试致电... mapGetter时,我在上方收到此错误,却一无所获。 我尝试遵循nuxt指南,但找不到有关如何使用mapGetters的信息。

这是外壳程序错误:

Syntax Error: Unexpected token (37:4)                                                                                 friendly-errors 21:58:14

  35 | 
  36 |   computed() {
> 37 |     ...mapGetters(['currentUser'])
     |     ^
  38 |   },
  39 | 
  40 |   methods: {

                                                                                                                      friendly-errors 21:58:14
 @ ./pages/index.vue?vue&type=script&lang=js& 1:0-227 1:243-246 1:248-472 1:248-472
 @ ./pages/index.vue
 @ ./.nuxt/router.js
 @ ./.nuxt/index.js
 @ ./.nuxt/client.js
 @ multi ./node_modules/@nuxt/components/lib/installComponents.js eventsource-polyfill webpack-hot-middleware/client?reload=true&timeout=30000&ansiColors=&overlayStyles=&path=%2F__webpack_hmr%2Fclient&name=client ./.nuxt/client.js

这是我的商店代码:

// store/user.js

export const state = () => ({
  user: {},
})

export const getters = {
  currentUser: (state) => {
    return state.user
  },
}

export const actions = {
  doLogin: ({ commit }, params) => {
    const mock = { username: params.username, token: '123' }

    commit('setUser', mock)
  },
}

export const mutations = {
  setUser: (state, user) => {
    state.user = user
  },
}

1 个答案:

答案 0 :(得分:0)

首先,compute是一个属性,而不是一个函数

export default {
   computed: {
      /***/
   }
}

然后,您正在使用模块(在您的情况下为user)系统来启动商店。映射商店时,还需要提及模块名称(期望index.js),就像这样

computed: {
     ...mapGetters("user", ["currentUser"])
   }