mapGetters不是index.js存储文件

问题描述 投票:0回答:1

我想从store/app.js文件中映射getter。我在这个文件中有一个sidebar() getter。在我的组件文件中,我调用:

export default {

  // ...

  computed: {
    ...mapGetters({
      sidebar: 'app/sidebar'
    }),

    // ...

    isCollapse() {
      return !this.sidebar.opened
    }
  }
}

但看起来它不起作用,我收到了TypeError: Cannot read property 'opened' of undefined

store/app.js

// ...

export const state = () => ({
  sidebar: {
    opened: true
  }
})

export const mutations = {
  // ...
}

export const actions = {
  // ...
}

export const getters = {
  sidebar(state) {
    return state.sidebar
  }
}
vue.js store vuex nuxt.js
1个回答
1
投票

我不熟悉nuxt,所以这只是一个想法。

我没有看到您的设置有任何问题。粗略地看一下https://nuxtjs.org/guide/vuex-store意味着可能需要一个你没有提及的store/index.js文件。

如果这不起作用,我建议将其添加到您的组件以帮助调试:

created() {
  console.log(this.$store); // <-- this is the vuex store accessed by the component
}
© www.soinside.com 2019 - 2024. All rights reserved.