首页 > 其他 > 详细

Vue3.0简单替代Vuex

时间:2021-03-29 17:35:56      阅读:103      评论:0      收藏:0      [点我收藏+]

共享状态必须符合两个条件:

  • 响应式:当状态改变时,使用它们的组件也应更新
  • 可用性:可以在任何组件中访问状态
import { reactive, provide, inject, readonly } from ‘vue‘

export const stateSymbol = Symbol(‘state‘)
export const createStore = () => {
    const state = reactive({
        count: 0
    });
    const increment = () => state.count++;
    return {
        increment,
        state: readonly(state)
    }
}
export const useState = () => inject(stateSymbol)
export const provideState = () => provide(createStore)

Vue3.0简单替代Vuex

原文:https://www.cnblogs.com/zhenjianyu/p/14592885.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!