我想為我的 VueX 商店實作自動重繪 功能。
用戶重繪 瀏覽器的所有內容,都會觸發 VueX 商店中的操作以從 API 呼叫加載用戶組態檔。
不可能做到這一點嗎?
import apiService from "@/services/apiService";
import apiUrls from "@/services/apiUrls";
import { getToken } from "@/services/jwtService";
// Code to run actions when user refresh
getToken() !== null ? this.actions.getUserProfile() : "";
const state = {
userProfile: {},
};
const getters = {
userProfile: (state) => state.userProfile,
};
const actions = {
async getUserProfile({ commit }) {
console.log("here");
try {
let response = await apiService.get(apiUrls.PROFILE);
commit("setUserProfile", response.data.data);
} catch (error) {
console.log(error);
}
},
};
謝謝你。
uj5u.com熱心網友回復:
用戶重繪 意味著應用程式將被重新執行。所以基本上main.js會被重新執行,App.vue重新創建等。
這意味著只需在任何頂級組件main.js的生命周期掛鉤中或其中呼叫您的代碼。created
頂級組件是指在應用程式早期創建的任何組件
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/478079.html
