高端网站改版,办公室装修设计软件,网络售后服务,无锡企业做网站如何在 Vue 3 中使用组合式 API 与 Vuex 进行状态管理的详细教程。
安装 Vuex
首先#xff0c;在你的 Vue 3 项目中安装 Vuex。可以使用 npm 或 yarn#xff1a;
npm install vuexnext --save
# or
yarn add vuexnext创建 Store
在 Vue 3 中#xff0c;你可以使用 creat…如何在 Vue 3 中使用组合式 API 与 Vuex 进行状态管理的详细教程。
安装 Vuex
首先在你的 Vue 3 项目中安装 Vuex。可以使用 npm 或 yarn
npm install vuexnext --save
# or
yarn add vuexnext创建 Store
在 Vue 3 中你可以使用 createStore 函数来创建 Vuex Store。通常我们会在项目的 src 目录下创建一个 store 文件夹并在其中创建一个 index.js 文件
// src/store/index.jsimport { createStore } from vuex;const store createStore({state() {return {count: 0};},mutations: {increment(state) {state.count;}},actions: {increment({ commit }) {commit(increment);}},getters: {doubleCount(state) {return state.count * 2;}}
});export default store;在 Vue 应用中使用 Store
在你的 Vue 应用中你需要将创建的 Store 实例传递给 Vue 应用。通常在 main.js 文件中进行
// src/main.jsimport { createApp } from vue;
import App from ./App.vue;
import store from ./store;const app createApp(App);app.use(store);app.mount(#app);使用 State
你可以在组合式 API 中通过 useStore 函数来访问 Vuex Store
templatedivp{{ count }}/p/div
/templatescript
import { computed } from vue;
import { useStore } from vuex;export default {setup() {const store useStore();const count computed(() store.state.count);return {count};}
};
/script使用 Getters
同样你可以在组合式 API 中使用 useStore 函数来访问 Vuex Getters
templatedivp{{ doubleCount }}/p/div
/templatescript
import { computed } from vue;
import { useStore } from vuex;export default {setup() {const store useStore();const doubleCount computed(() store.getters.doubleCount);return {doubleCount};}
};
/script提交 Mutations
你可以在组合式 API 中通过 useStore 函数来提交 Vuex Mutations
templatedivp{{ count }}/pbutton clickincrementIncrement/button/div
/templatescript
import { computed } from vue;
import { useStore } from vuex;export default {setup() {const store useStore();const count computed(() store.state.count);const increment () {store.commit(increment);};return {count,increment};}
};
/script分发 Actions
你可以在组合式 API 中通过 useStore 函数来分发 Vuex Actions
templatedivp{{ count }}/pbutton clickincrementIncrement/button/div
/templatescript
import { computed } from vue;
import { useStore } from vuex;export default {setup() {const store useStore();const count computed(() store.state.count);const increment () {store.dispatch(increment);};return {count,increment};}
};
/script模块化 Store
当应用变得非常复杂时可以将 store 分割成模块module。每个模块拥有自己的 state、mutation、action 和 getter甚至可以嵌套子模块
// src/store/index.jsimport { createStore } from vuex;const moduleA {state() {return {count: 0};},mutations: {increment(state) {state.count;}},actions: {increment({ commit }) {commit(increment);}},getters: {doubleCount(state) {return state.count * 2;}}
};const store createStore({modules: {a: moduleA}
});export default store;访问模块中的 State
在组合式 API 中你可以通过模块名来访问模块中的 state 和其他属性
templatedivp{{ count }}/p/div
/templatescript
import { computed } from vue;
import { useStore } from vuex;export default {setup() {const store useStore();const count computed(() store.state.a.count);return {count};}
};
/script
文章转载自: http://www.morning.fbqr.cn.gov.cn.fbqr.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn http://www.morning.sqqds.cn.gov.cn.sqqds.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.fbmzm.cn.gov.cn.fbmzm.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.fjlsfs.com.gov.cn.fjlsfs.com http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.wslr.cn.gov.cn.wslr.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.lsmgl.cn.gov.cn.lsmgl.cn http://www.morning.zqdzg.cn.gov.cn.zqdzg.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.ljcjc.cn.gov.cn.ljcjc.cn http://www.morning.yfpnl.cn.gov.cn.yfpnl.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.jxzfg.cn.gov.cn.jxzfg.cn http://www.morning.nzmw.cn.gov.cn.nzmw.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.zcsyz.cn.gov.cn.zcsyz.cn http://www.morning.dqbpf.cn.gov.cn.dqbpf.cn http://www.morning.lxmks.cn.gov.cn.lxmks.cn http://www.morning.chfxz.cn.gov.cn.chfxz.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.trkl.cn.gov.cn.trkl.cn http://www.morning.mrqwy.cn.gov.cn.mrqwy.cn http://www.morning.kwcnf.cn.gov.cn.kwcnf.cn http://www.morning.hxpsp.cn.gov.cn.hxpsp.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.jnzfs.cn.gov.cn.jnzfs.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.ybgyz.cn.gov.cn.ybgyz.cn http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.mbdbe.cn.gov.cn.mbdbe.cn http://www.morning.syhwc.cn.gov.cn.syhwc.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.xnbd.cn.gov.cn.xnbd.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.yrpd.cn.gov.cn.yrpd.cn