feat: 先写好userStore

This commit is contained in:
Burt 2023-12-23 11:27:00 +08:00
parent da5284331f
commit fef57184a6
4 changed files with 47 additions and 1 deletions

View File

@ -4,6 +4,8 @@
<view class="text-area">
<text class="title">{{ title }}</text>
</view>
<button @click="setUserInfo">设置UserInfo</button>
<button @click="clearUserInfo">清除UserInfo</button>
<view class="flex justify-center items-center text-blue-500">
Demo Count: {{ countStore.count }}
<button class="ml-2" @click="countStore.increment">新增</button>
@ -22,13 +24,22 @@
<script setup lang="ts" name="TestIndex">
import { ref } from 'vue'
import { useCountStore } from '@/store/count'
import { useCountStore, useUserStore } from '@/store'
const countStore = useCountStore()
const title = ref('Hello')
const uniLayout = ref()
console.log(uniLayout)
const userStore = useUserStore()
const setUserInfo = () => {
userStore.setUserInfo({ username: 'fly', token: 'abcdef' })
}
const clearUserInfo = () => {
userStore.clearUserInfo()
}
</script>
<style>

View File

@ -13,3 +13,7 @@ store.use(
)
export default store
// 模块统一导出
export * from './user'
export * from './count'

27
src/store/user.ts Normal file
View File

@ -0,0 +1,27 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { UserInfo } from '../typings'
export const useUserStore = defineStore(
'user',
() => {
const userInfo = ref<UserInfo>()
const setUserInfo = (val: UserInfo) => {
userInfo.value = val
}
const clearUserInfo = () => {
userInfo.value = undefined
}
return {
userInfo,
setUserInfo,
clearUserInfo,
}
},
{
persist: true,
},
)

4
src/typings.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
export type UserInfo = {
username: string
token: string
}