From bf691cef2774a3f22702aadc82a50e2070e18b21 Mon Sep 17 00:00:00 2001 From: SuJiehao Date: Thu, 8 Dec 2022 14:46:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20pinia=20=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/main.ts | 2 ++ src/stores/.gitkeep | 0 src/stores/index.ts | 7 +++++++ src/stores/modules/member.ts | 25 +++++++++++++++++++++++++ 5 files changed, 35 insertions(+) delete mode 100644 src/stores/.gitkeep create mode 100644 src/stores/index.ts create mode 100644 src/stores/modules/member.ts diff --git a/package.json b/package.json index 3ae9967..6c08cb6 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "@dcloudio/uni-mp-toutiao": "3.0.0-alpha-3061120221205002", "@dcloudio/uni-mp-weixin": "3.0.0-alpha-3061120221205002", "@dcloudio/uni-quickapp-webview": "3.0.0-alpha-3061120221205002", + "pinia": "^2.0.27", "vue": "^3.2.45", "vue-i18n": "^9.1.9" }, diff --git a/src/main.ts b/src/main.ts index 4b5dc40..1651699 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,11 @@ import { createSSRApp } from 'vue' +import pinia from './stores' import App from './App.vue' export function createApp() { const app = createSSRApp(App) + app.use(pinia) return { app, } diff --git a/src/stores/.gitkeep b/src/stores/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/stores/index.ts b/src/stores/index.ts new file mode 100644 index 0000000..68fa234 --- /dev/null +++ b/src/stores/index.ts @@ -0,0 +1,7 @@ +import { createPinia } from 'pinia' + +// 创建pinia实例 +const pinia = createPinia() + +// 导出pinia实例,给main使用 +export default pinia diff --git a/src/stores/modules/member.ts b/src/stores/modules/member.ts new file mode 100644 index 0000000..a0a29de --- /dev/null +++ b/src/stores/modules/member.ts @@ -0,0 +1,25 @@ +import { defineStore } from 'pinia' +import { ref } from 'vue' + +// 定义 Store +export const useMemberStore = defineStore('member', () => { + // 会员信息 + const profile = ref() + + // 保存会员信息,登录时使用 + const setProfile = (val: any) => { + profile.value = val + } + + // 清理会员信息,退出时使用 + const clearProfile = () => { + profile.value = undefined + } + + // 记得 return + return { + profile, + setProfile, + clearProfile, + } +})