web前端全系列 教程
1839个小节阅读:2244.5k
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
Actions 相当于组件中的 methods。 它们可以使用 defineStore()
中的 actions
属性定义,并且它们非常适合定义业务逻辑
xxxxxxxxxx
import { defineStore } from "pinia"
export const useCountStore = defineStore("count", {
state: () => {
return {
count: 10
}
},
getters: {
getCount: (state) => "当前Count:" + state.count,
doubleCount(state) {
return this.getCount + state.count
}
},
actions: {
increment() {
this.count++
},
decrement() {
this.count--
}
}
})
xxxxxxxxxx
<template>
<h3>Pinia</h3>
<p>Count1:</p>
<button @click="addCountHandler">增加</button>
<button @click="minCountHandler">减少</button>
</template>
<script setup>
import { useCountStore } from "../store/count"
const store = useCountStore();
function addCountHandler(){
store.increment()
}
function minCountHandler(){
store.decrement()
}
</script>
xxxxxxxxxx
<template>
<p>Count2:</p>
<p>Count2:</p>
<button @click="addCountHandler">增加</button>
<button @click="minCountHandler">减少</button>
</template>
<script>
import { mapState,mapActions } from "pinia"
import { useCountStore } from "../store/count"
export default {
computed:{
mapState(useCountStore,{
myOwnName:"count",
count:store => store.count,
doubleCount(store){
return store.count * 2
}
})
},
methods:{
mapActions(useCountStore,["increment","decrement"]),
addCountHandler(){
this.increment()
},
minCountHandler(){
this.decrement()
}
}
}
</script>
实时效果反馈
1. 下列代码中,画横线的地方填写的是:
xxxxxxxxxx
<template>
<p>Count2:</p>
<p>Count2:</p>
<button @click="addCountHandler">增加</button>
<button @click="minCountHandler">减少</button>
</template>
<script>
import { mapState,mapActions } from "pinia"
import { useCountStore } from "../store/count"
export default {
computed:{
mapState(useCountStore,{
myOwnName:"count",
count:store => store.count,
doubleCount(store){
return store.count * 2
}
})
},
methods:{
___(useCountStore,["increment","decrement"]),
addCountHandler(){
this.increment()
},
minCountHandler(){
this.decrement()
}
}
}
</script>
A mapState
B mapGetter
C mapMutation
D mapActions
答案
1=>D