首页 文章

vuex“store”vs“data:store”在Vue()构造函数中,哪个最好?

提问于
浏览
0

Vue文档提到在构造函数上使用"data"选项,以保留全局/共享数据:https://vuejs.org/v2/guide/state-management.html

这是有道理的 .

Vuex文档传递"store"对象,但没有属性名称:https://github.com/vuejs/vuex/blob/dev/examples/counter/app.js

new Vue({
  el: '#app',
  store,
  render: h => h(Counter)
})

不应该那样

new Vue({
  el: '#app',
  data: store,
  render: h => h(Counter)
})

其他示例将其传递为"store: store" https://ypereirareis.github.io/blog/2017/04/25/vuejs-two-way-data-binding-state-management-vuex-strict-mode/

但"store"不是文件属性:https://vuejs.org/v2/api/

1 回答

  • 2

    在Vue实例上使用 store 只是简写

    store: store

    https://ariya.io/2013/02/es6-and-object-literal-property-value-shorthand

    在主实例上设置 store 是Vuex的一部分,以及Vuex如何与您的商店进行交互,因此需要 .

    如果您在没有Vuex的情况下使用自己的全局状态设置,那么将您自己的商店添加到数据中是完全正确的 . 事实上,当不需要像Vuex这样的完整设置时,许多应用程序都会这样做 .

相关问题