其他前端状态管理工具整理
https://www.yuque.com/wuzhao/kb/xb192z
pinia读写
https://pinia.esm.dev/core-concepts/state.html#accessing-the-state
pinia 不用像vuex一样异步请求需要action—>mutation—>修改state;
可以通过 store 实例访问状态,直接读写状态
const store = useStore()
store.counter++
actions
Pinia 没有 Mutations,统一在 actions 中操作 state,通过this.xx 访问相应状态,虽然可以直接操作 Store,但还是推荐在 actions 中操作,保证状态不被意外改变,action 和普通的函数一样,action 同样可以像 Getter 一样访问其他的 Store,同上方式使用其它 Store。
export const useStore({
state: ()=> ({
count: 2,
// ...
})
// ...
actinos: {
countPlusOne() {
this.count++;
},
countPlus(num) {
this.count += num;
}
}
})
import { useStore } from "../store/store";
// ...
function addByActions() {
store.countPlus(100);
}
pinia目录
store可以散落在各个文件夹内
只要是useStore引入对应的store就好
<template>
<div>test inner pinia</div>
<div>count:{{ store.name }}</div>
</template>
<script lang="ts">
import { useStore } from "./store";
export default {
setup() {
const store = useStore();
return {
store,
};
},
};
</script>