web前端全系列 教程
1839个小节阅读:2243.9k
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
actions
可以是异步的,这也是使用 actions
最大的原因
xxxxxxxxxx
import { defineStore } from "pinia"
export const useCountStore = defineStore("count", {
state: () => {
return {
count: 10,
banner:[]
}
},
actions: {
increment(num) {
this.count += num
},
decrement(num) {
this.count -= num
}
}
})
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(5)
}
function minCountHandler(){
store.decrement(5)
}
</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(10)
},
minCountHandler(){
this.decrement(20)
}
}
}
</script>
xxxxxxxxxx
import { defineStore } from "pinia"
import axios from "axios"
export const useBannerStore = defineStore("banner", {
state: () => {
return {
banner:[]
}
},
actions: {
setBanner(url){
axios.get(url)
.then(res =>{
this.banner = res.data.banner
}).catch(error =>{
console.log(error);
})
}
}
})
xxxxxxxxxx
<template>
<h3>网络请求</h3>
<ul>
<li v-for="(item,index) in store.banner" :key="index">
<h3></h3>
<p></p>
</li>
</ul>
</template>
<script setup>
import { onMounted } from "vue"
import { useBannerStore } from "../store/banner"
const store = useBannerStore();
onMounted(() =>{
store.setBanner("http://iwenwiki.com/api/blueberrypai/getIndexBanner.php")
})
</script>