Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store add Getters #23

Open
wants to merge 1 commit into
base: web23
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/kstore/kvuex.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,38 @@ class Store {

this._mutations = options.mutations
this._actions = options.actions
this._getters = options.getters || {}

this.commit = this.commit.bind(this)
this.dispatch = this.dispatch.bind(this)

this.getters = {}
this.defineReactive() // 将传入的getters配置自定义响应式
// defineProperty(this.getters, 'doubleCounter', {get(){}})
// 最后还能利用vue计算属性做缓存
}

// 天王盖地虎
defineReactive() {
const getters = this._getters
let result = null
Object.keys(getters).forEach(key => {
Object.defineProperty(this.getters, key, {
get: () => {
const entry = getters[key]
if (!entry) {
console.error('unkown getters key');
}
result = entry.call(this, this.state)
if (result === undefined) {
console.error('getters must be return value');
}
return result
}
})
})
}

get state() {
return this._vm._data.$$state
}
Expand Down
Loading