vuex "store" vs "data: store" in Vue() constructor, which is best?

时间:2017-08-04 13:09:23

标签: vue.js vuejs2 vuex

Vue docs mention to use "data" option on constructor, to keep global/shared data: https://vuejs.org/v2/guide/state-management.html

This makes sense.

Vuex docs passes the "store" object, without a property name though: https://github.com/vuejs/vuex/blob/dev/examples/counter/app.js

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

Shouldn't that have been

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

?

Other examples pass it as "store: store" https://ypereirareis.github.io/blog/2017/04/25/vuejs-two-way-data-binding-state-management-vuex-strict-mode/

but "store" isn't a documented property: https://vuejs.org/v2/api/

1 个答案:

答案 0 :(得分:2)

在Vue实例上使用store只是

的简写

store: store

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

在您的主要实例上设置store是Vuex的一部分,以及Vuex如何与您的商店进行互动。

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