1. uniapp内置了pinia,无需手动安装
2. main.js中集成
import App from './App'
import * as Pinia from 'pinia';
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
app.use(Pinia.createPinia());
return {
app,
Pinia,
}
}
3. 封装(根目录下创建一个store文件夹,store文件夹中创建一个js文件用于封装store)
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
state(){
return {
count:10
}
},
actions: {
increment() {
this.count++
},
},
getters:{
num:(state)=>state.count
}
});
4. 页面.vue文件中引入并使用
<template>
<view class="g-container">
详情页
<view class="">
{{counterStore.count}}
</view>
<button type="default" @click="counterStore.increment()">加一</button>
</view>
</template>
<script setup>
import { ref } from "vue"
import { onLoad } from '@dcloudio/uni-app'
import { useCounterStore } from '../../store/counter.js'
const counterStore = useCounterStore()
</script>
<style lang="scss"></style>