添加全局状态管理
This commit is contained in:
parent
bc699caa10
commit
c985af60a3
@ -6,6 +6,15 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import BasicLayout from "./layouts/BasicLayouts.vue"
|
import BasicLayout from "./layouts/BasicLayouts.vue"
|
||||||
|
import { healthUsingGet } from '@/api/mainController.ts'
|
||||||
|
import {useLoginUserStore} from "@/stores/useLoginUserStore.ts";
|
||||||
|
|
||||||
|
const loginUserStore = useLoginUserStore()
|
||||||
|
loginUserStore.fetchLoginUser()
|
||||||
|
|
||||||
|
healthUsingGet().then((res) => {
|
||||||
|
console.log(res)
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
@ -19,7 +19,12 @@
|
|||||||
</a-col>
|
</a-col>
|
||||||
<a-col flex="120px">
|
<a-col flex="120px">
|
||||||
<div class="user-login-status">
|
<div class="user-login-status">
|
||||||
<a-button type="primary" href="/user/login">登录</a-button>
|
<div v-if="loginUserStore.loginUser.id">
|
||||||
|
{{ loginUserStore.loginUser.userName ?? '无名' }}
|
||||||
|
</div>
|
||||||
|
<div v-else>
|
||||||
|
<a-button type="primary" href="/user/login">登录</a-button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</a-col>
|
</a-col>
|
||||||
</a-row>
|
</a-row>
|
||||||
@ -30,6 +35,9 @@
|
|||||||
import { h, ref } from 'vue'
|
import { h, ref } from 'vue'
|
||||||
import { HomeOutlined } from '@ant-design/icons-vue'
|
import { HomeOutlined } from '@ant-design/icons-vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
|
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
|
||||||
|
|
||||||
|
const loginUserStore = useLoginUserStore()
|
||||||
|
|
||||||
const items = [
|
const items = [
|
||||||
{
|
{
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
import { ref, computed } from 'vue'
|
|
||||||
import { defineStore } from 'pinia'
|
|
||||||
|
|
||||||
export const useCounterStore = defineStore('counter', () => {
|
|
||||||
const count = ref(0)
|
|
||||||
const doubleCount = computed(() => count.value * 2)
|
|
||||||
function increment() {
|
|
||||||
count.value++
|
|
||||||
}
|
|
||||||
|
|
||||||
return { count, doubleCount, increment }
|
|
||||||
})
|
|
26
src/stores/useLoginUserStore.ts
Normal file
26
src/stores/useLoginUserStore.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { defineStore } from 'pinia'
|
||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
export const useLoginUserStore = defineStore('loginUser', () => {
|
||||||
|
const loginUser = ref<any>({
|
||||||
|
userName: '未登录',
|
||||||
|
})
|
||||||
|
|
||||||
|
async function fetchLoginUser() {
|
||||||
|
// todo 由于后端还没提供接口,暂时注释
|
||||||
|
// const res = await getCurrentUser();
|
||||||
|
// if (res.data.code === 0 && res.data.data) {
|
||||||
|
// loginUser.value = res.data.data;
|
||||||
|
// }
|
||||||
|
// 测试用户登录,3 秒后自动登录
|
||||||
|
setTimeout(() => {
|
||||||
|
loginUser.value = { userName: '测试用户', id: 1 }
|
||||||
|
}, 3000)
|
||||||
|
}
|
||||||
|
|
||||||
|
function setLoginUser(newLoginUser: any) {
|
||||||
|
loginUser.value = newLoginUser
|
||||||
|
}
|
||||||
|
|
||||||
|
return { loginUser, setLoginUser, fetchLoginUser }
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user