主题
vuex4
准备
引包
shellnpm install vuex --save
在 vue 实例中使用
jsimport store from './store' app.use(store)
仓库模板
store.js
typescript
import { createStore } from "vuex";
export interface StoreProps {
user: string | null;
}
export default createStore({
state: {
user: null
},
mutations: {
login (state, user) {
state.user = user
}
},
actions: {},
modules: {}
})
使用
- 通过
useStore
获取 Store 实例
js
import { computed } from 'vue'
import { useStore } from 'vuex'
import { StoreProps } from './store'
const { state, commit } = useStore<StoreProps>()
const user = computed(() => state.user) // 要用 computed 形成响应式变化
commit('addCount')