vuex方案
安装vuex@4.x
两个重要变动:
- 去掉了构造函数
Vuex,而使用createStore创建仓库
- 为了配合
composition api,新增useStore函数获得仓库对象
global state
由于vue3的响应式系统本身可以脱离组件而存在,因此可以充分利用这一点,轻松制造多个全局响应式数据

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
import { reactive, readonly } from "vue"; import * as userServ from "../api/user";
const state = reactive({ user: null, loading: false });
export const loginUserStore = readonly(state);
export async function login(loginId, loginPwd) { state.loading = true; const user = await userServ.login(loginId, loginPwd); state.loginUser = user; state.loading = false; }
export async function loginOut() { state.loading = true; await userServ.loginOut(); state.loading = false; state.loginUser = null; }
export async function whoAmI() { state.loading = true; const user = await userServ.whoAmI(); state.loading = false; state.loginUser = user; }
|
Provide&Inject
在vue2中,提供了provide和inject配置,可以让开发者在高层组件中注入数据,然后在后代组件中使用

除了兼容vue2的配置式注入,vue3在composition api中添加了provide和inject方法,可以在setup函数中注入和使用数据

考虑到有些数据需要在整个vue应用中使用,vue3还在应用实例中加入了provide方法,用于提供整个应用的共享数据
1 2 3 4
| creaetApp(App) .provide("foo", ref(1)) .provide("bar", ref(2)) .mount("#app");
|

因此,我们可以利用这一点,在整个vue应用中提供共享数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
import { readonly, reactive, inject } from "vue"; const key = Symbol();
export function provideStore(app) { const state = reactive({ user: null, loading: false }); async function login(loginId, loginPwd) { state.loading = true; const user = await userServ.login(loginId, loginPwd); state.loginUser = user; state.loading = false; } async function loginOut() { state.loading = true; await userServ.loginOut(); state.loading = false; state.loginUser = null; } async function whoAmI() { state.loading = true; const user = await userServ.whoAmI(); state.loading = false; state.loginUser = user; } app.provide(key, { state: readonly(state), login, loginOut, whoAmI, }); }
export function useStore(defaultValue = null) { return inject(key, defaultValue); }
import { provideStore as provideLoginUserStore } from "./useLoginUser";
export default function provideStore(app) { provideLoginUserStore(app); }
import { createApp } from "vue"; import provideStore from "./store"; const app = createApp(App); provideStore(app); app.mount("#app");
|
对比
|
vuex |
global state |
Provide&Inject |
| 组件数据共享 |
✅ |
✅ |
✅ |
| 可否脱离组件 |
✅ |
✅ |
❌ |
| 调试工具 |
✅ |
❌ |
✅ |
| 状态树 |
✅ |
自行决定 |
自行决定 |
| 量级 |
重 |
轻 |
轻 |