Vuex:_this.$store 未定义

Vuex: _this.$store is undefined

将 Vuex 添加到我的项目后,我无法在任何组件中访问 this.$store。错误信息是

TypeError: _this.$store is undefined

我已经查看了一堆试图解决这个问题的问题,但据我所知,我做的一切都是正确的。谁能帮忙?我正在使用 vue-cli webpack 作为我的项目基础

main.js:

import Vue from 'vue';
import resource from 'vue-resource';
import router from './router';
import store from './store/index.js';

import App from './App';
import Home from './components/Home';
import NavButton from './components/atoms/NavButton';

Vue.use(resource);
Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App, Home, NavButton },
  template: '<App/>'
})

/store/index.js:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const state = {
    isWriting: false,
    isLoggedIn: false,
}

const getters = {
    isWriting: state => {
        return state.isWriting;
    }
}

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

App.vue

...
import NavBar from '@/components/organisms/NavBar';
export default {
  name: 'App',
  components: { NavBar },
  created: () => {
    console.log(this.$store.state.isLoggedIn); // THIS LINE
  }
}
...

package.json

...
"dependencies": {
    "vue": "^2.5.2",
    "vue-resource": "^1.3.6",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1"
  },
...

已解决:

在创建时使用粗箭头不正确,应该是 created: function() {...}

当使用箭头函数时 "this" 不会像您期望的那样成为 Vue 实例,因为箭头函数绑定到父上下文。相反,

    created() { //function body. "this" will be the Vue instance},
    mounted() {//function body. "this" will be the Vue instance},
    methods: { someFunc() {}, async someAsyncFunc {} }

我把店铺从app.js搬到/store/index.js后也遇到了这个问题

我必须在提交

中将 state.store.myValue 更改为 store.myValue