解决pinia问题

This commit is contained in:
yuanteng 2024-10-28 17:56:03 +08:00
parent 1bb5c652c7
commit fb5f6d9ea7
5 changed files with 20 additions and 12 deletions
src

View File

@ -2,7 +2,7 @@
import axios from "axios";
const myAxios = axios.create({
withCredentials:true,
baseURL: 'http://127.0.0.1:8888/api'//测试服务器
baseURL: 'http://localhost:8888/api'//测试服务器
});
// 添加请求拦截器
axios.interceptors.request.use(function (config) {

View File

@ -132,7 +132,6 @@
const logout = async () => {
const res: any = await myAxios.post("/user/logout",{})
console.log(res)
if(res.data.code == 1 && res.data) {
store.$reset()
await router.replace('/')

View File

@ -6,8 +6,10 @@ export const userStore = defineStore( 'user' , {
state: () => {
return {
loginUser: {
userName: '未登录',
id: 0,
phone: '',
userAvatar: '',
userName: '未登录',
userRole: 'notLogin'
}
}
@ -15,13 +17,11 @@ export const userStore = defineStore( 'user' , {
//actions是用于定义和处理状态更改的方法。它们可以包含任意的逻辑如异步请求、数据处理和状态更新。
actions: {
//获取登录用户信息
async getLoginUser() {
async getLoginUser(res:any) {
//请求登录信息
const res = await myAxios.get('/user/get')
// console.log(res.data)
if( res.data.code === 1 && res ?.data ) {
this.updateUser( res.data )
}
// const res = await myAxios.get('/user/get')
console.log(res,121212312312)
this.updateUser( res )
},
//更新用户信息
//更新state

View File

@ -75,6 +75,7 @@
</template>
</el-table-column>
</el-table>
<button @click="pri">aaa</button>
</template>
<script setup lang="ts">
@ -82,6 +83,9 @@ import { ref , reactive} from 'vue'
import { ElMessageBox } from 'element-plus'
import { ElMessage } from 'element-plus'
import type { UploadFile } from 'element-plus';
import { userStore } from '../../store/userStore';
const store = userStore()
const dialogImageUrl = ref('');
const disabled = ref(false);
@ -96,6 +100,11 @@ const form = ref({
tips: ''
})
const pri =()=>{
console.log(store.loginUser);
}
const handleSelectionChange = (row: any) => {
selectedItems.value = JSON.parse(JSON.stringify(row))
}

View File

@ -54,11 +54,11 @@
userAccount: username.value,
userPassword: password.value
})
// console.log(res.data.code)
console.log(res.data.code)
if(res.data.code === 1 && res ?.data) {
//pinia
await store.getLoginUser()
//
await store.getLoginUser(res.data.data)
//
await router.replace('/PersonalCenter')
}
}