vuex从安装到使用的教程

简介:
vuex的安装
 npm install vuex --save或cnpm install vuex --save

 
 
  • 1
main.js引入vuex
import Vue from 'vue'
import Vuex from 'vuex'
import store from './vuex/store'

Vue.use(Vuex)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
vuex的目录结构和store.js的代码

这里写图片描述

store.js的代码(异步和同步的代码均留下两个方法)
import Vue from 'vue'
import Vuex from 'vuex'
import GLOBAL_CONFIG from '../config/config'
import axios from 'axios'
import BScroll from 'better-scroll'
// import BScroll from '../assets/js/iscroll'

Vue.use(Vuex)

const menu = {
  state: {
    mannumber:[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,'其它'],
    activeindex: null,
    nowindex:null,
    products: {},
    GLOBAL_CONFIG:GLOBAL_CONFIG['GLOBAL_CONFIG']
  },
  mutations: {
    get_product: function (state, products) {
        state.products = products;
        for(let i = 0; i < state.products.length; i++){
            if(state.products[i]['image'] != false && state.products[i]['image'].indexOf(state.GLOBAL_CONFIG['dataImage']) === -1){
                state.products[i]['image'] = state.GLOBAL_CONFIG['base64Header'] + state.products[i]['image'];
            }else{
                state.products[i]['image'] = require("../assets/file.png");
            }
        }
    }
  },
  actions: {

  }
}

export default new Vuex.Store({
    state: {
        mastdisplay: false,
        supervisory_control:false,
        product: {},
        countNumber:{},
        orderdetail:{},
        GLOBAL_CONFIG:GLOBAL_CONFIG['GLOBAL_CONFIG']    
    },
    mutations: {
        mastdisplay: function (state,name) {
            if(typeof name !== 'string'){              
                if(name['name'] == 'mastdisplay' || name['name'] == 'mastspecificat' || name['name'] == 'mastflavour'){
                    if(name['product'].hasOwnProperty('specs')){
                        for(let i = 0; i < name['product']['specs'].length; i++){
                            name['product']['specs'][i]['number'] = 0;
                        }
                    }                   
                    state.product = name['product'];
                    if(name['name'] === 'mastspecificat'){
                        let specificatscroll = new BScroll('.mast-scrollspecificat',{
                            scrollY: true,
                            click: true
                        });
                    }
                    if(name['name'] === 'mastflavour'){
                        let flavouringscroll = new BScroll('.mast-flavouringscroll',{
                            scrollY: true,
                            click: true
                        });
                    }                                        
                }
                name = name['name'];            
            }
            if(state[name]){
                state[name] = false;
            }else{
                state[name] = true;
            }

        },
        countNumber: function (state,countNumber) {
            state.countNumber = countNumber;
        },
        get_orderdetail: function (state,response) {
            state.orderdetail = response;
            console.dir(state.orderdetail);
        },
        hidden_customcount: function (state) {            
            if(state.mastload){
                state.mastload = false;
            }
        }
    },
    components:{

    },
    modules: {
        menu:menu
    },
    actions: {
        get_orderdetail: function (context,company,name) {
            axios.post('/point-api-order_detail',{
                tableid:company['tableid'],
                companyid:company['companyid']
            }).then(function(response){           
                context.commit('get_orderdetail',response);
            }, function(response){
                mui.toast('网络错误');
            });
        },
        check_cart: function (context,product) {
            axios.post('/point-api-checkcart',{
                user_id:product['user_id'],
                url:context.state.GLOBAL_CONFIG['WECHAT_API_HOST']+"/wechat/app/index.php?i="+localStorage.i+"&c=entry&do=ShopCart&m=wei_scan_code"
            }).then(function(response){               
                if(response.hasOwnProperty('error')){
                    mui.toast(response['erron']);
                    return false;
                }else{
                    context.commit('set_cart',response);
                    // let menuscroll = new BScroll('.mast-cart-content-parent',{
                    //     scrollY: true,
                    //     click: true
                    // });
                } 
            }, function(response){
                mui.toast('网络错误');
            });
        }
})

 
 
  • 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
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
mutations的使用,可以在mutations里面定义方法,里面的方法为同步操作,方法里的第一个参数是state对象,可操作state对象里定义的属性,第二个参数为传进来的参数,也可不传,在组件里可以直接操作state里的属性,例如:
this.$store.state.mastdisplay 或通过store里的方法改变this.$store.commit('mastdisplay');

 
 
  • 1
actions的使用,actions里面的方法用来处理异步操作,调用异步接口后,获取到数据,对state里的属性进行重新赋值,然后在组件里计算该属性的变化
computed: {
    orderdetail: function () {
      return this.$store.state.orderdetail;
    }
    mastdisplay: function () {
      return this.$store.state.mastdisplay;
    }
    products: function () {
      return this.$store.state.menu.products;
    },
    product: function () {
      return this.$store.state.product;
    }
  }

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
在组件里调用actions方法
this.$store.dispatch('get_orderdetail',{
        tableid:localStorage.tableid,
        companyid:localStorage.companyid
      });

 
 
  • 1
  • 2
  • 3
  • 4
有时候我们可能会把一个组件分为一个模块,例如上面的代码,我定义了一个menu的模块,那么我需要在modules里面引入这个模块,在组件里操作该模块的属性如下:

this.$store.state.menu.products 

调用该模块的mutations里的方法不需要加上menu.

原文发布时间:
原文作者:一念执着。c
本文来源CSDN博客如需转载请紧急联系作者

相关文章
|
弹性计算 人工智能 Cloud Native
阿里云无门槛和有门槛优惠券解析:学生券,满减券,补贴券等优惠券领取与使用介绍
为了回馈用户与助力更多用户节省上云成本,阿里云会经常推出各种优惠券相关的活动,包括无门槛优惠券和有门槛优惠券。本文将详细介绍阿里云无门槛优惠券的领取与使用方式,同时也会概述几种常见的有门槛优惠券,帮助用户更好地利用这些优惠,降低云服务的成本。
1833 133
|
9月前
|
JavaScript Linux API
OpenClaw部署保姆级攻略:阿里云无影云电脑+本地系统+Skills集成+API配置实操手册
OpenClaw(又名Clawdbot)是2026年主流的开源AI自动化代理工具,主打本地优先、隐私可控、任务可执行的核心特性,区别于普通对话式AI,可通过集成各类Skills技能,完成文件处理、网页自动化、办公辅助、信息提取等实际操作,无需依赖第三方平台托管。本文聚焦2026年最新部署方案,完整覆盖阿里云无影云电脑云端部署、本地Windows11/MacOS/Linux系统部署两大核心场景,同步详解Skills技能安装与管理、阿里云百炼Coding Plan免费大模型API配置流程,搭配高频问题排查方案,帮助零基础用户全程顺畅完成部署与使用,全程无额外付费门槛,操作可复现。
628 1
|
10月前
|
人工智能 弹性计算 自然语言处理
2026年阿里云一键部署使用OpenClaw详细教程
本文将详细介绍如何通过阿里云轻量应用服务器实现OpenClaw的一键部署,助力大家快速拥有专属AI数字员工。
847 2
|
10月前
|
机器学习/深度学习 人工智能 算法
PPO算法全解:让AI“学步”更稳的强化学习秘诀
本文用“教孩子骑车”比喻,生动解析PPO算法如何通过“信任区域”约束与Clipping裁剪机制,实现稳定高效的强化学习。避开复杂数学,讲清其在RLHF、大模型对齐中的核心作用,并提供可运行代码与调参指南。(239字)
|
9月前
|
人工智能 移动开发 自然语言处理
阿里云多端低代码开发平台魔笔是什么?如何建站?魔笔怎么收费?2026最新整理魔笔百科
阿里云魔笔(Mobi)是AI+低代码多端应用开发平台,融合通义千问大模型,支持拖拽搭建Web/小程序/H5/App页面,AI自动生成文案、图片、SQL,内置50+行业模板及BaaS服务,一键发布上线。零代码门槛,5分钟建站,适合业务人员、运营、产品经理等非技术人员使用。(239字)
787 17
|
9月前
|
编解码 Linux iOS开发
Qt 框架进行跨平台客户端外包开发
Qt 6.x 跨平台外包开发成熟可靠,支持 Windows/macOS/Linux/移动/嵌入式。涵盖需求分析、Figma→QML原型、单代码库架构、多端适配、CI/CD自动构建、全平台打包签名及完整交付物。适配数字孪生可结合Qt Quick 3D。(240字)
|
12月前
|
存储 编解码 前端开发
网站开发之图片格式对比与选择
本文介绍了网页常用图片格式的特点与优化策略,涵盖位图(如JPEG、PNG、GIF)和矢量图(如SVG)的原理、优缺点及适用场景,并提供WebP等新型格式对比。同时总结了图像压缩、响应式加载、雪碧图、Base64内联等前端优化技巧,帮助在保证视觉效果的前提下提升网页性能。
2286 1
|
人工智能 小程序 算法
婚恋交友源码系统,uniapp跨端适配,php稳定后台,小程序+h5+app
基于TP6+Uni-app开发,支持多端同步,提供完整源码及部署指南。涵盖智能匹配、AI/真人红娘服务、多元沟通方式,助力高效交友。
534 0
|
人工智能 自然语言处理 供应链
超越聊天:AI代理——下一代人机交互的雏形
超越聊天:AI代理——下一代人机交互的雏形
916 22
|
移动开发 算法 5G
面向高多普勒衰落信道的 OTFS 调制技术
正交时频空(OTFS)调制通过延迟-多普勒域传输,将高多普勒信道转化为准静态环境,显著提升高速移动下的通信鲁棒性。本文研究其信号检测与信道估计,提出低复杂度MCMC检测算法和基于PN导频的稀疏信道估计算法,验证了OTFS在高速场景下的优异性能。
1110 0
面向高多普勒衰落信道的 OTFS 调制技术