使用方式一
一、安装pinia, 并在main.js引入
npm install pinia
import { createPinia } from 'pinia'
var app = createApp(App)
app.use(createPinia())
app.mount('#app')
二、创建store 创建文件tabbarStore.js
import {defineStore} from 'pinia'
import {ref} from 'vue'
const useTabbarStore = defineStore("tabbar",()=>{
const isTabbarShow = ref(true)
const change = (value)=>{
isTabbarShow.value = value
}
return {
isTabbarShow,
change
}
})
export default useTabbarStore
三。vue文件中使用
<script setup>
import { onMounted,onBeforeMount,onBeforeUnmount } from 'vue';
import useTabbarStore from '../store/tabbarStore';
const store = useTabbarStore()
onBeforeMount(()=>{
store.change(false)
})
onBeforeUnmount(()=>{
store.change(true)
})
</script>
使用方式二
一、安装pinia, 并在main.js引入
npm install pinia
import { createPinia } from 'pinia'
var app = createApp(App)
app.use(createPinia())
app.mount('#app')
二、创建store 创建文件tabbarStore.js
import {defineStore} from 'pinia'
const useTabbarStore = defineStore("tabbar",{
state:()=>({
isTabbarShow:true
}),
actions:{
change(value){
this.isTabbarShow = value
}
}
})
export default useTabbarStore
三、vue文件中使用
<script setup>
import { onBeforeMount,onBeforeUnmount } from 'vue';
import useTabbarStore from '../store/tabbarStore';
const store = useTabbarStore()
onBeforeMount(()=>{
store.$patch({
isTabbarShow:false
})
})
onBeforeUnmount(()=>{
store.$patch({
isTabbarShow:true
})
})
</script>