Vue3.0简单替代Vuex 共享状态必须符合两个条件:
响应式:当状态改变时,使⽤它们的组件也应更新
可⽤性:可以在任何组件中访问状态
import { reactive, provide, inject, readonly } from 'vue'
export const stateSymbol = Symbol('state')
export const createStore = () => {
const state = reactive({
count: 0
});
const increment = () => unt++;
return {
increment,
state: readonly(state)
}
}
export const useState = () => inject(stateSymbol)
vue中reactiveexport const provideState = () => provide(createStore)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。