【发布时间】:2023-02-14 11:39:13
【问题描述】:
使用 Fetch 而不是 axios,我想在我的组件中接收这样的数据:
const userStore = useAuthStore();
const { user, error, loading } = storeToRefs(userStore);
userStore.getMe();
但我不知道该怎么做。 我希望在一行中直接显示错误、数据和加载状态,因为我认为这样更好。
但我不想在商店中声明这样的加载:
export const useAuthStore = defineStore({
id: "auth",
state: () => ({
user: {} as User,
loading: false,
}),
因为如果我调用另外一个和这个store相关的方法(User),也是一样的Loading状态。所以这个loading状态(甚至是error状态)都会有冲突。
如果我使用 Javascript 而不使用 Typescript,我肯定会在获取或出错时(在商店中)像这样替换 this.user :
async getMe() {
this.user = { loading: true };
try {
return await AuthService.getMe();
} catch (error) {
if (error) {
his.user = { error };
}
}
},
因为它是 TypeScript,所以我无法像设置接口那样替换“用户”状态。
我想要的只是返回与唯一操作相关的数据、错误、加载(与状态无关)。
授权商店:
import { defineStore } from "pinia";
import AuthService from "@/api/modules/auth";
interface User {
email: string;
first_name: string;
last_name: string;
force_password_change: boolean;
groups: string[];
has_2fa_enabled: boolean;
is_staff: boolean;
lang: string;
last_password_change: string;
permissions: string[];
session_expiry_date: string;
uid: string;
}
export const useAuthStore = defineStore({
id: "auth",
state: () => ({
user: {} as User,
loading: false,
}),
actions: {
async getMe() {
// this.user = { loading: true };
try {
return await AuthService.getMe();
} catch (error) {
if (error) {
// this.user = { error };
}
}
},
},
});
服务:
import { Api } from "../apiSettings";
class AuthService {
async getMe(): Promise {
return await Api.get("api/auth/me/");
}
}
export default new AuthService();
应用程序.vue:
-
我相信你正在寻找的是这里描述的 - michaelzanggl.com/articles/vue-cleaning-up-components