共享状态必须符合两个条件:
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)
原文:https://www.cnblogs.com/zhenjianyu/p/14592885.html