当前位置: 首页 > 知识库问答 >
问题:

vue3 pinia store里怎么定义对象?并保持响应式?

江英华
2024-01-11

pinia中,想定义一个对象userinfostate,可以用reactive定义吗?并保持响应性?这种写法是可以的吗?应该怎么正确使用呢?

userinfo={    userid:'',    name:'',    phone:'',    avatar:''}
export const useUserStore = defineStore('user', {     const userinfo = reactive({      userid:'',      name:'',      phone:'',      avatar:''    })  return { userinfo }})

共有2个答案

宇文飞羽
2024-01-11
import { reactive } from 'vue'import { defineStore } from 'pinia'export const useUserStore = defineStore('userinfo', () => {  const userinfo = reactive({    userId: '',    name: '',    phone: '',    avatar: ''  })  return { userinfo }})
苗阳
2024-01-11

看pinia的官方文档:

https://pinia.vuejs.org/zh/core-concepts/
https://pinia.vuejs.org/zh/core-concepts/state.html
import { defineStore } from 'pinia'const useStore = defineStore('storeId', {  // 为了完整类型推理,推荐使用箭头函数  state: () => {    return {      // 所有这些属性都将自动推断出它们的类型      count: 0,      name: 'Eduardo',      isAdmin: true,      items: [],      hasChanged: true,    }  },})
 类似资料: