vuejs怎么结合pinia插件,下面web建站小编给大家详细介绍一下!
1、安装pinia脚手架
yarn add pinia -S
2、main.js引入
import {createApp} from "vue"
import App from "./app.vue"
import store from "./store/index.js"
const app = createApp(App);
const store = createPinia();
app.use(store).mount("#app")
3、在store文件夹下新建test.js
import {definePinia} from "pinia"
export default testStore = definePinia('testId',{
  state:()=>{
    tname:"test",
    tnum:0,
  },
  getters:{
     changeTnum(){
       console.log("getters")
       this.tnum++;
     }
  },
  actions:{
     addNum(val){
       this.tnum += val
     }
  },
  //持久化存储配置
  presist:{
    enable:true,
    strategies:[
       {
       key:"testId",
       storage:localStorage,
       paths:['tnum']
       } 
    ]
  }
})
4、在store文件夹下新建index.js
import {createPinia} from "pinia"
const store = createPinia();
export default store
5、新建vue组件
<template>
  <div>
    <div> {{tname}}</div>
    <div> {{tid}}</div>
    <div> tnum: {{tnum}}</div>
    <div> {{tchangeNum}}</div>
    <div><button @click="tchangeName">修改</button></div>
    <div> <button @click="treset">重置</button></div>
    <div @click="actionsBtn">actionsBtn</div>
  </div>
</template>
<script setup>
import { storeToRefs } from 'pinia'
import { useStore } from '../store/user'
import { useTest } from '../store/test.js'
const testStore = useTest();
let { tname, tchangeNum, tnum } = storeToRefs(testStore)
</script>
//直接修改数据
tchangeName(){
  tname.value = "测试数据";
  tnum.value++;
}
//当然也可以使用`$path`批量修改
tchangeName(){
  testStore.$path(state=>{
    state.tname = "测试数据";
    state.value = 7;
  })
}
上面是“vuejs怎么结合pinia插件”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_2697.html
workflows工作流
 一只黑色的小猫在童话森林里嗅着一朵发光的外星花 一只黑色的小猫在童话森林里嗅着一朵发光的外星花
 一只猫捧着一条鱼ComfyUI工作流 一只猫捧着一条鱼ComfyUI工作流
 3D圆形玉雕图案comfyui工作流 3D圆形玉雕图案comfyui工作流
 奋斗的小黄人ComfyUI工作流 奋斗的小黄人ComfyUI工作流
 一幅梦幻家园comfyui工作流 一幅梦幻家园comfyui工作流
 汉堡里的一只毛茸茸的小猫ComfyUI工作流 汉堡里的一只毛茸茸的小猫ComfyUI工作流
 一个男人正走进科幻的大门ComfyUI工作流 一个男人正走进科幻的大门ComfyUI工作流
 冬天的严寒里红梅枝上停留着一只鸟 冬天的严寒里红梅枝上停留着一只鸟
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

 3D彩色卡片
 3D彩色卡片 利用css3做一个动态loading效果
 利用css3做一个动态loading效果 3d文字360度旋转
 3d文字360度旋转 js+css3做一个灯泡开灯关灯效果
 js+css3做一个灯泡开灯关灯效果 利用css绘画棋盘布局(象棋)
 利用css绘画棋盘布局(象棋) iframe开发admin后台
 iframe开发admin后台 自动打字效果(惊喜在后面)
 自动打字效果(惊喜在后面) 纯css3绘制的小鸟
 纯css3绘制的小鸟










