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

vue简易store #71

Open
conan1992 opened this issue Sep 9, 2020 · 0 comments
Open

vue简易store #71

conan1992 opened this issue Sep 9, 2020 · 0 comments

Comments

@conan1992
Copy link
Owner

使用 Vuex 可能是繁琐冗余的。确实是如此——如果您的应用够简单,您最好不要使用 Vuex。一个简单的 store 模式就足够您所需了

简单状态管理

  • 新建store.js
var store = {
  debug: true,
  state: {
    message: 'Hello!'
  },
  setMessageAction (newValue) {
    if (this.debug) console.log('setMessageAction triggered with', newValue)
    this.state.message = newValue
  },
  clearMessageAction () {
    if (this.debug) console.log('clearMessageAction triggered')
    this.state.message = ''
  }
}
export default store
  • 在A、B兄弟组件中import store from '@/store.js'
  • 组件数据通过store.state进行绑定
  • 约定:组件不允许直接变更属于 store 实例的 state,而应执行 action(setMessageAction) 来分发 (dispatch) 事件通知 store 去改变;这样约定的好处是,我们能够记录所有 store 中发生的 state 变更

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant