this.$store与store的区别

时间:2021-08-22 11:24:07 类型:vue
字号:    

如果你在根组件下注入了store那么所有的.vue文件里使用就可以直接用 this.$store.xxxx;而在js文件里面如果想要使用store,就必须先引入import store from '@/store'然后使用store.xxx,因为js里面是打印不出来this.$store的

  vue官网是这么说的:

  为了在 Vue 组件中访问 this.$store property,你需要为 Vue 实例提供创建好的 store。Vuex 提供了一个从根组件向所有子组件,以 store 选项的方式“注入”该 store 的机制:

//main.jsimport store from './store'new Vue({
  el: '#app',
  store,})


<