1. let Vue;
    2. const install = (_Vue) => {
    3. // 先判断是否use过
    4. if (Vue && _Vue === Vue) {
    5. console.error('[vuex] already installed. Vue.use(Vuex) should be called only once.')
    6. return
    7. }
    8. // 绑定Vue的构造函数
    9. Vue = _Vue
    10. // 判断Vue版本,2版本推荐用mixin
    11. // 1版本单例,放到原型上(会影响其他Vue实例)
    12. const version = Number(Vue.version.split('.')[0])
    13. if (version >= 2) {
    14. Vue.mixin({
    15. beforeCreate() {
    16. const options = this.$options
    17. // store injection
    18. if (options.store) {
    19. this.$store = typeof options.store === 'function'
    20. ? options.store()
    21. : options.store
    22. } else if (options.parent && options.parent.$store) {
    23. this.$store = options.parent.$store
    24. }
    25. }
    26. })
    27. } else {
    28. // override init and inject vuex init procedure
    29. // for 1.x backwards compatibility.
    30. const _init = Vue.prototype._init
    31. Vue.prototype._init = function (options = {}) {
    32. options.init = options.init
    33. ? [vuexInit].concat(options.init)
    34. : vuexInit
    35. _init.call(this, options)
    36. }
    37. }
    38. }
    39. class Store {
    40. construtor(options = {}) {
    41. this._vm = new Vue({
    42. data: {
    43. stage: options.state,
    44. }
    45. })
    46. let getters = options.getters;
    47. this.getters = {}
    48. Object.keys(getters).forEach(getterName => {
    49. Object.defineProperty(this.getters, getterName, {
    50. get: () => getters[getterName](this.state)
    51. enumerable: true, // for local getters
    52. })
    53. })
    54. }
    55. get state () {
    56. return this._vm._data.$$state
    57. }
    58. set state (v) {
    59. if (__DEV__) {
    60. assert(false, `use store.replaceState() to explicit replace store state.`)
    61. }
    62. }
    63. commit = (type, payload, options) => {
    64. }
    65. }
    66. export default {
    67. Store,
    68. install,
    69. }