如果你在根組件下注入了store那么所有的.vue文件里使用就可以直接用 this.$store.xxxx;而在js文件里面如果想要使用store,就必須先引入import store from '@/store'然后使用store.xxx,因?yàn)閖s里面是打印不出來this.$store的
vue官網(wǎng)是這么說的:
為了在 Vue 組件中訪問 this.$store property,你需要為 Vue 實(shí)例提供創(chuàng)建好的 store。Vuex 提供了一個(gè)從根組件向所有子組件,以 store 選項(xiàng)的方式“注入”該 store 的機(jī)制:
//main.jsimport store from './store'new Vue({ el: '#app', store,})