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工作流
stvmccrr风格的玫瑰花ComfyUI工作流
一个巨大的漆黑的蟹王ComfyUI工作流
一架令人难忘的美丽钢琴ComfyUI工作流
一轮月亮悬挂在树上的天空中ComfyUI工作流
一张科幻照片,火星车在沙漠里ComfyUI工作流
三阶放大comfyui工作流
嘴唇丰满的漂亮女人
丛林里坐着一尊佛像
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

javascript如何利用draggable实现一个拖拽效果
用svg画出游泳池动画效果
js实现下雪特效
css3搭积木叠加图形
canvas经线动画走到效果
css3结合svg做一个动态广告
3D彩色卡片
css3绘制一个会动的大嘴鸟










