企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] > [pinia.vuejs.org](https://pinia.vuejs.org/zh/introduction.html#basic-example) ## Pinia 状态管理推荐是使用 Pinia,而不是vuex ## 安装 ``` yarn add pinia # 或者使用 npm npm install pinia ``` ## 语法 在组合式 API 中 * `ref()`就是`state`属性 * `computed()`就是`getters` * `function()`就是`actions` ## 技巧 ### 全局通知 设置 ``` const selfUser = computed(() => { return users.value[userId()] }) ``` 引用 ``` const {user} = storeToRefs(userStore()); 设置为动态更新 ``` ### computed 带参数的动态更新 ``` const getUser = computed(() => { return (user_id: string) => { if (users.value[user_id]) { return users.value[user_id] } else { throw new RuntimeException(`not found the user_id=${user_id}`) } } }) ``` 引用 ``` const {getUser} =storeToRefs(useUsers()) // 测试 cong getUser 中获取的值带有响应式 getUser.value(props.user_id).user_id ```