vue3-pinia-ts項(xiàng)目中的使用示例詳解
store.ts
import { defineStore } from "pinia";
import { GlobalState, ThemeConfigProp } from "./interface";
import { createPinia } from "pinia";
import piniaPersistConfig from "@/config/piniaPersist";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
// defineStore 調(diào)用后返回一個(gè)函數(shù),調(diào)用該函數(shù)獲得 Store 實(shí)體
export const GlobalStore = defineStore({
// id: 必須的,在所有 Store 中唯一
id: "GlobalState",
// state: 返回對(duì)象的函數(shù)
state: (): GlobalState => ({
// token
token: "",
// userInfo
userInfo: "",
// element組件大小
assemblySize: "default",
// language
language: "",
// themeConfig
themeConfig: {
// 默認(rèn) primary 主題顏色
primary: "#409EFF",
// 深色模式
isDark: false,
// 灰色模式
isGrey: false,
// 色弱模式
isWeak: false,
// 面包屑導(dǎo)航
breadcrumb: true,
// 標(biāo)簽頁(yè)
tabs: true,
// 頁(yè)腳
footer: true
}
}),
getters: {},
actions: {
// setToken
setToken(token: string) {
this.token = token;
},
// setUserInfo
setUserInfo(userInfo: any) {
this.userInfo = userInfo;
},
// setAssemblySizeSize
setAssemblySizeSize(assemblySize: string) {
this.assemblySize = assemblySize;
},
// updateLanguage
updateLanguage(language: string) {
this.language = language;
},
// setThemeConfig
setThemeConfig(themeConfig: ThemeConfigProp) {
this.themeConfig = themeConfig;
}
},
persist: piniaPersistConfig("GlobalState")
});
// piniaPersist(持久化)
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);
export default pinia;main.ts
import { createApp } from "vue";
import App from "./App.vue";
import pinia from "@/store/index";
...
const app = createApp(App);
app.use(router).use(I18n).use(pinia).use(directives).use(ElementPlus).mount("#app");頁(yè)面使用
<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import { Login } from "@/api/interface";
import { CircleClose, UserFilled } from "@element-plus/icons-vue";
import type { ElForm } from "element-plus";
import { ElMessage } from "element-plus";
import { loginApi } from "@/api/modules/login";
import { GlobalStore } from "@/store";
import { MenuStore } from "@/store/modules/menu";
import { TabsStore } from "@/store/modules/tabs";
import md5 from "js-md5";
const globalStore = GlobalStore();
const menuStore = MenuStore();
const tabStore = TabsStore();
// 定義 formRef(校驗(yàn)規(guī)則)
type FormInstance = InstanceType<typeof ElForm>;
const loginFormRef = ref<FormInstance>();
const loginRules = reactive({
username: [{ required: true, message: "請(qǐng)輸入用戶名", trigger: "blur" }],
password: [{ required: true, message: "請(qǐng)輸入密碼", trigger: "blur" }]
});
// 登錄表單數(shù)據(jù)
const loginForm = reactive<Login.ReqLoginForm>({
username: "",
password: ""
});
const loading = ref<boolean>(false);
const router = useRouter();
// login
const login = (formEl: FormInstance | undefined) => {
if (!formEl) return;
formEl.validate(async valid => {
if (!valid) return;
loading.value = true;
try {
const requestLoginForm: Login.ReqLoginForm = {
username: loginForm.username,
password: md5(loginForm.password)
};
const res = await loginApi(requestLoginForm);
// * 存儲(chǔ) token
globalStore.setToken(res.data!.access_token);
// * 登錄成功之后清除上個(gè)賬號(hào)的 menulist 和 tabs 數(shù)據(jù)
menuStore.setMenuList([]);
tabStore.closeMultipleTab();
ElMessage.success("登錄成功!");
router.push({ name: "home" });
} finally {
loading.value = false;
}
});
};
// resetForm
const resetForm = (formEl: FormInstance | undefined) => {
if (!formEl) return;
formEl.resetFields();
};
onMounted(() => {
// 監(jiān)聽(tīng)enter事件(調(diào)用登錄)
document.onkeydown = (e: any) => {
e = window.event || e;
if (e.code === "Enter" || e.code === "enter" || e.code === "NumpadEnter") {
if (loading.value) return;
login(loginFormRef.value);
}
};
});
</script>到此這篇關(guān)于vue3-pinia-ts項(xiàng)目中的使用的文章就介紹到這了,更多相關(guān)vue3-pinia-ts使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決webpack-bundle-analyzer的問(wèn)題大坑
這篇文章主要介紹了解決webpack-bundle-analyzer的問(wèn)題大坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
在Vue3中實(shí)現(xiàn)子組件向父組件傳遞數(shù)據(jù)的代碼示例
Vue3作為目前最熱門(mén)的前端框架之一,以其輕量化、易用性及性能優(yōu)勢(shì)吸引了大量開(kāi)發(fā)者,在開(kāi)發(fā)過(guò)程中,不可避免地需要在組件之間傳遞數(shù)據(jù),本文將詳細(xì)講解在Vue3中如何實(shí)現(xiàn)子組件向父組件傳遞數(shù)據(jù),并通過(guò)具體示例代碼使概念更加清晰2024-07-07
在vue中實(shí)現(xiàn)表單驗(yàn)證碼與滑動(dòng)驗(yàn)證功能的代碼詳解
在Web應(yīng)用程序中,表單驗(yàn)證碼和滑動(dòng)驗(yàn)證是常見(jiàn)的安全機(jī)制,用于防止惡意攻擊和機(jī)器人攻擊,本文將介紹如何使用Vue和vue-verify-code庫(kù)來(lái)實(shí)現(xiàn)表單驗(yàn)證碼和滑動(dòng)驗(yàn)證功能,需要的朋友可以參考下2023-06-06
vue項(xiàng)目實(shí)現(xiàn)背景顏色以及下劃線從左到右漸變動(dòng)畫(huà)效果
這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)背景顏色以及下劃線從左到右漸變動(dòng)畫(huà)效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
vue項(xiàng)目中運(yùn)用webpack動(dòng)態(tài)配置打包多種環(huán)境域名的方法
本人分享一個(gè)vue項(xiàng)目里,根據(jù)命令行輸入不同的命令,打包出不同環(huán)境域名的方法。需要的朋友跟隨小編一起看看吧2019-06-06
Vue中實(shí)現(xiàn)在線畫(huà)流程圖的方法
最近在調(diào)研一些在線文檔的實(shí)現(xiàn),包括文檔編輯器、在線思維導(dǎo)圖、在線流程圖等,本文分享在Vue框架下基于metaeditor-mxgraph實(shí)現(xiàn)在線流程圖,感興趣的朋友一起看看吧2024-07-07
vue-cli單頁(yè)應(yīng)用改成多頁(yè)應(yīng)用配置詳解
本篇文章主要介紹了vue-cli單頁(yè)應(yīng)用改成多頁(yè)應(yīng)用配置詳解,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Vue3中無(wú)法為el-tree-select設(shè)置反選問(wèn)題解析
這篇文章主要介紹了Vue3中無(wú)法為el-tree-select設(shè)置反選問(wèn)題分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04

