vuejs怎么结合pinia
插件,下面编程教程网小编给大家详细介绍一下!
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 内容,请继续关注编程基础学习网。
织梦狗教程
本文标题为:vuejs怎么结合pinia插件


基础教程推荐
猜你喜欢
- vuejs如何实现htmlEncode编码和htmlDecode解码 2025-01-14
- 使用Layui表格实现PHP数据获取的方法详解 2023-08-31
- javascript开发随笔一 preventDefault的必要 2023-12-02
- el-table实现拖拽行排序sortablejs 2025-01-13
- reactjs-nginx尝试根据uri在目录中查找index.html 2023-10-25
- html个人笔记 2023-10-27
- Vue 配置脚手架CLI 3 2023-10-08
- 高效利用Angular中内置服务$http、$location等 2024-01-07
- Ajax jsonp跨域请求实现方法 2022-10-18
- React Redux应用示例详解 2024-02-08