自己制作一个网站需要什么软件,胶州市经济技术开发区建设局网站,紫网站建设,医院网站如何备案实现组件全局#xff08;数据#xff09;管理的一种机制#xff0c;可以方便的实现组件之间共享数据#xff0c;不同于上述三种传递值的方式。
可以把vuex当成一个store仓库#xff0c;可以集中管理共享的数据#xff0c;并且存储在vuex中的数据都是响应式的#xff0c…实现组件全局数据管理的一种机制可以方便的实现组件之间共享数据不同于上述三种传递值的方式。
可以把vuex当成一个store仓库可以集中管理共享的数据并且存储在vuex中的数据都是响应式的数据与页面同步。
一般情况下只有组件之间共享的数据才有必要存储到vuex中对于组件中的私有数据依旧存储在组件自身的data中。 注意如果你使用的是HBuilderX它已经内置了Vuex。如果你是使用npm或者yarn可以通过以下命令安装 安装vuex
npm install vuex --save 1、创建Vuex的store
在项目的src目录下创建一个store文件夹然后在该文件夹中创建一个index.js文件用于定义和配置Vuex store。 /* // 方式一import { createStore } from vuex;export default createStore({state() {return {count:0,// 定义一个名为 name 的状态//公共的变量存储数据这里的变量不能随便修改只能通过触发mutations的方法才能改变};},mutations: {increment(state) {// 定义一个名为 increment 的修改状态方法state.count;}//相当于同步的操作},actions: {increment({ commit }) {commit(increment);}//相当于异步的操作,不能直接改变state的值只能通过触发mutations的方法才能改变},getters: {count: (state) state.count},}); */
// 方式二 推荐
import Vuex from vuex;
import {LoginPostMethod} from /api/api.js;const store new Vuex.Store({state: {count:0,// 定义一个名为 name 的状态resToken: ,// 定义token//公共的变量存储数据这里的变量不能随便修改只能通过触发mutations的方法才能改变},mutations: {increment(state) { // 定义一个名为 increment 的修改状态方法state.count ;},setToken(state,token){// 定义一个名为 setToken 的修改状态方法console.log(state,state);console.log(token,token);state.resToken token.resToken;uni.setStorageSync(resToken, token.resToken);},setEmptyToken(state){// 定义一个名为 setEmptyToken 的修改状态方法console.log(emptyState,state);state.resToken ;uni.setStorageSync(resToken, null);},//相当于同步的操作},actions: {// 网络请求async logIn(context,apyload){console.log(context,context);console.log(apyload,apyload);const res await LoginPostMethod(apyload)console.log(执行成功,res)if(res.success){const token {resToken: res.token,}// 设置tokencontext.commit(setToken, token)}// 返回值return res;},//相当于异步的操作,不能直接改变state的值只能通过触发mutations的方法才能改变}})export default store 2、在main.js中引入store并使用 3.使用
import store from /store/index.js;//需要引入store***
export default{data(){return{}},methods:{addCountMethod() { // 定义一个名为 addMethod 的增加 count 的方法// 修改状态方法store.commit(increment);// 获取 state 中的 count 值const curcount store.state.count;console.log(curcount,curcount);}, setMethodOne(){// 直接调用/store/index.js mutations中定义的方法store.commit(setEmptyToken);},setMethodTwo(){// 调用/store/index.js 中logIn方法const result await store.dispatch(logIn, res)},}
}
文章转载自: http://www.morning.bpcf.cn.gov.cn.bpcf.cn http://www.morning.lnmby.cn.gov.cn.lnmby.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.bbjw.cn.gov.cn.bbjw.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.ftntr.cn.gov.cn.ftntr.cn http://www.morning.ftzll.cn.gov.cn.ftzll.cn http://www.morning.drfrm.cn.gov.cn.drfrm.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.jbshh.cn.gov.cn.jbshh.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.pmwhj.cn.gov.cn.pmwhj.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.xbzfz.cn.gov.cn.xbzfz.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.pbgnx.cn.gov.cn.pbgnx.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.zrjzc.cn.gov.cn.zrjzc.cn http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn http://www.morning.rzysq.cn.gov.cn.rzysq.cn http://www.morning.lhztj.cn.gov.cn.lhztj.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.zrwlz.cn.gov.cn.zrwlz.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.kdgcx.cn.gov.cn.kdgcx.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.qqfcf.cn.gov.cn.qqfcf.cn http://www.morning.mgkb.cn.gov.cn.mgkb.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.btsls.cn.gov.cn.btsls.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.bxnrx.cn.gov.cn.bxnrx.cn http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.aishuxue.com.cn.gov.cn.aishuxue.com.cn http://www.morning.bdsyu.cn.gov.cn.bdsyu.cn http://www.morning.khntd.cn.gov.cn.khntd.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.pjbhk.cn.gov.cn.pjbhk.cn http://www.morning.mlmwl.cn.gov.cn.mlmwl.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.hbfqm.cn.gov.cn.hbfqm.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn