pinia中数据解构

hykeda3年前Vue2573

在vuex中获取state中的数据可以解构的方式如下:

import { mapState,mapMutations } from "vuex"
computed:{
    ...mapState(['userInfo']),
},

代码中可以直接使用userInfo了

在pinia中如果类似的进行解构:

import { useTestStore } from './store'
const Test = useTestStore()
const { current, name } = Test

如进行这样的解构,解构出来的current和name不是响应性的,无法进行动态的修改

应该使用 storeToRefs 进行解构处理

import { storeToRefs } from 'pinia'
 
const Test = useTestStore()
 
const { current, name } = storeToRefs(Test)


这样机构出来的state变量是具有响应性的,可以随意使用。

相关文章

vite配置代理proxy实现跨域问题

最近在开发vite+vue项目时,调用接口获取服务器数据时提示接口有跨域问题,解决办法是配置代理:export default defineConfig({  //配置代理  se...

直接在浏览器中使用 vue3+element-plus中icons的方法

首先引入文件,如果用cdn的方式:<script src="//unpkg.com/@element-plus/icons-vue"></script&...

使用vite打包vue项目生成dist文件夹,部署至nginx除了index.html,其他路径都提示404错误的解决

使用vite打包vue项目生成dist文件夹,部署至nginx除了index.html,其他路径都提示404错误的解决

用vue打包生成dist文件夹,然后在本地nginx部署vue项目时只能访问默认页面,刷新和跳转页面都会出问题。原因:只访问了dist文件下的 index.html、index.htm页面,...

vite.config.js配置@

import { defineConfig } from 'vite'import vue from '@vitejs/plugin-vue'import path f...

vue3中defineProps传值使用ref响应式失效详解

子组件接收父组件的传参。父组件:<template> <Son :data="data"/> </template> <s...

vite+vue 插件/组件集合

自动引入插件:https://github.com/antfu/unplugin-auto-importvue的按需组件自动导入:https://github.com/antfu/unplugin-v...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。