store 和 [this.]$store
简单来说,如果你在根组件下注入了store那么所有的.vue文件里使用就可以直接用 this.$store.xxxxVue官网:为了在 Vue 组件中访问 this.$store.property,你需要为 Vue 实例提供创建好的 store。Vuex 提供了一个从根组件向所有子组件,以 store 选项的方式“注入”该 store 的机制
//main.jsimport store from './store'new Vue({el: '#app',store, //根组件注入store})
//index.vuegetData() {return {userId: this.$store.state.user.userId,......}}
而在js文件里面如果想要使用store,就必须先引入import store from '@/store’然后使用store.xxx,因为js里面是打印不出来this.$store的
// src/test.js文件import store from './store/';console.log(store)console.log(this) // undefinedconsole.log(this.$store) // 会报错