From 6ef61ba1e6c7c7b156637a41d2a58aeb82547364 Mon Sep 17 00:00:00 2001 From: mrjzhang Date: Wed, 13 Nov 2019 22:27:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(omiv):=20Omiv.install=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=20store=20=E4=BE=9D=E8=B5=96=E6=B7=BB=E5=8A=A0=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=20mixins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/omiv/src/vue.js | 68 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/packages/omiv/src/vue.js b/packages/omiv/src/vue.js index 53ed29422..5273a8105 100644 --- a/packages/omiv/src/vue.js +++ b/packages/omiv/src/vue.js @@ -183,15 +183,81 @@ export function install(_Vue) { } function applyMixin(Vue) { - Vue.mixin({ beforeCreate: omivInit }) + const omivComputed = { + state() { + if (isMultiStore) { + let state = {} + Object.keys(this.$store).forEach(k => { + state[k] = store[k].data + }) + return state + } + return this.$store.data + }, + store() { + return this.$store + } + } function omivInit() { const options = this.$options + const use = options.use + const useSelf = options.useSelf + if (options.store) { this.$store = typeof options.store === 'function' ? options.store() : options.store } else if (options.parent && options.parent.$store) { this.$store = options.parent.$store } + + if (isMultiStore) { + if (use) { + let updatePath = {} + for (let storeName in use) { + getPath(use[storeName], updatePath, storeName) + this.$store[storeName].components.push(this) + } + this.__$updatePath_ = updatePath + } + + if (useSelf) { + let updateSelfPath = {} + for (let storeName in useSelf) { + getPath(useSelf[storeName], updateSelfPath, storeName) + this.$store[storeName].updateSelfComponents.push(this) + } + this.__$updateSelfPath_ = updateSelfPath + } + } else { + if (use) { + // { count: true } + this.__$updatePath_ = getPath(use) + // 依赖 store 的组件,压入了一个 vm 实例 + this.$store.components.push(this) + } + if (useSelf) { + this.__$updateSelfPath_ = getPath(useSelf) + this.$store.updateSelfComponents.push(this) + } + } } + + function omivDestroyed() { + if (isMultiStore) { + for (let key in this.$store) { + removeItem(this, this.$store[key].components) + removeItem(this, this.$store[key].updateSelfComponents) + } + } else { + removeItem(this, this.$store.updateSelfComponents) + removeItem(this, this.$store.components) + } + } + + Vue.mixin({ + beforeCreate: omivInit, + computed: omivComputed, + destroyed: omivDestroyed + }) }