feat: 先写好userStore
This commit is contained in:
parent
da5284331f
commit
fef57184a6
@ -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>
|
||||
|
@ -13,3 +13,7 @@ store.use(
|
||||
)
|
||||
|
||||
export default store
|
||||
|
||||
// 模块统一导出
|
||||
export * from './user'
|
||||
export * from './count'
|
||||
|
27
src/store/user.ts
Normal file
27
src/store/user.ts
Normal 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
4
src/typings.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
export type UserInfo = {
|
||||
username: string
|
||||
token: string
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user