01===>
module的理解:将一个大的系统进行拆分 拆分成若干个细的模块
给个模块都有自己的 state mutations 等属性
这样可以在自己的小模块中进行修改 方便维护 module的简单使用
(1)创建main.js(首页)在store.js同级中 (2)store.js中引入 (3)modules的形式注册
在store.js中写
{
// Vuex 仓库文件(入口)
import Vue from 'vue' import Vuex from 'vuex' //全局注册Vue.use(Vuex) // 引入子模块(add)
import shopcar from "./ShopCar"
import main from "./main" // 创建一个状态厂库 并且将它暴露出去 export default
const store=new Vuex.Store({
modules:{
// key:(模块名) value(对应模块的配置)
shopcar, //它相当于把shopcar.js暴露的那个对象放置在这里
main
}
}) // 取各个模块的值
console.log(store.state.shopcar.name) //这样可以拿到可拿到 购物出的的name值为 “我的值是购物车”
console.log(store) //下面为输出的值
/*
state: Object
main: Object
shopcar: Object
*/
export default store
} 然后创建main.js(首页)
{
export default{
state: {
val: "主文件需要的值",
name: "我的值是主文件"
},
mutations:{ },
}
}
02====》如何在商家页面Merchant.vue 获取到 modules模块中--shangjia.js中state的数据

ps===>在main.js文件中  key值是不能够改名字的  value是引入进来的那个文件名
key:vaulue相同的话可以简写 main.js
{
//引入store实例
import storeaa from "./store/store"; new Vue({
router,
store: storeaa, //这里是key:value的形式 这里是不能够改变的哦 key 的固定的值是store value的值可以跟引入的实例对象一致即可
// 这这里注册store后,全局可以共享 store了
render: h => h(App)
}).$mount("#app"); } store.js
{
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) // 引入子模块 (千万别忘记了)
import shopcar from "./ShopCar"
import main from "./main"
import shangjia from './shangjia' // 创建一个状态厂库 并且将它暴露出去 export default
const store=new Vuex.Store({
modules:{
// key:(模块名) value(对应模块的配置)
shopcar, //它相当于把shopcar.js暴露的那个对象放置在这里
main,
shangjia
} })
export default store
} shangjia.js modules中管理商家模块的数据
{
export default {
state:{
val:"我是商家页面数据",
name:"哈哈哈商家"
}
}
} Merchant.vue获取shangjia.js中state的数据
{
<template>
<div>
<p>{{ test }}</p>
</div>
</template> export default {
data(){
return{
test:"",
}
}, created() {
this.test=this.$store.state.shangjia.val;
},
} }
03===》 利用computed:{}计算属性提高性能 例2不变
利用computed只要母体数据不发生改变 它就不会发生改变 添加 Merchant.vue中
{
将值渲染出来
<h2>为了提升性能 {{test1}}</h2> computed: {
test1() {
return this.$store.state.shangjia.name; //返回 “哈哈哈商家”
}
}, }
04===>将所有的数据放在store.js的data中
两个页面的代码一模一样 A页面点击加1 B页面数字同样发生改变 ps===>在利用modules模块来管理数据的时候 你需要在store.js 引入相应的子模块 如例2
如果将所有的数据 都放在store.js 的data中饭就不需要 引入相应的子模块 ps===> 只要你去修改state中的值 你就考虑写mutations A.vue页面 B.vue页面
{
<template>
<div>
<button @click="clickDec">-</button>
<span> {{ num }} </span>
<button @click="addNum">+</button>
</div>
</template> export default {
data() {
return {
test: ""
};
},
methods:{
addNum(){
// 提交一个mutations ,改变state中的值 相调用mutations中的addNum函数
this.$store.commit("addNum")
}, clickDec(){
this.$store.commit("clickDec")
}
}, computed:{
num(){
return this.$store.state.num
}
},
};
} store.js
{
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) // 创建一个状态厂库 并且将它暴露出去 export default
const store=new Vuex.Store({
state(){
return{
test:"我输测试数据",
num:0
}
}, mutations:{
addNum(state){
state.num++;
},
clickDec(state) {
state.num--;
},
} })
export default store }
5====》对例4进行优化    this.$store.commit("chang",1)    传参 判断出入的值正数还是负数  负数不能小于0

A页面 B页面  简化了代码
{
<button @click="clickDec">-</button>
<span> {{ num }} </span>
<button @click="addNum">+</button> methods:{
addNum(){
// 提交一个mutations ,改变state中的值 用了第一种方式
this.$store.commit("chang",1)
}, clickDec(){ //如果小于0 不执行改函数
if(this.$store.state.num==0){ //不能将这一条语句放在 最后 将没有意义
return;
}
this.$store.commit("chang",-1)
// console.log(this.$store.state.num)
}
},
} store.js 简化了
{
mutations: {
chang(state, zhi) {
state.num += zhi;
}
}
}

02vuex-modules的更多相关文章

  1. Play modules

    A Play application can be assembled from several application modules. This allows you to reuse appli ...

  2. YII的Modules模块化

    转载来源: http://blog.csdn.net/mengxiangbaidu/article/details/7041296 http://blog.csdn.net/colzer/articl ...

  3. 在Angular1.X中使用CSS Modules

    在Angular1.5中,增加了一个Component方法,并且定义了组件的若干生命周期hook,在代码规范中也是推崇组件化开发,但是很遗憾的是,CSS模块化组件化的问题并没有得到解决,大部分项目的打 ...

  4. 如何在Mac系统里面更新 Ansible 的 Extra Modules

    最近遇到一个问题 seport is not a legal parameter in an Ansible task or handler 原因是我本地 Ansible 的 Extra Module ...

  5. Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules

    在eclipse里面配置tomcat时候遇到的问题: Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web mo ...

  6. 安装ESXi5.5遇到Relocating modules and starting up the kernel的处理

    在一些Dell较旧的服务器上安装ESXi 5.x时, 会遇到卡在Relocating modules and starting up the kernel过不去的问题. 比如我装的这台CS24VSS. ...

  7. PHPCMS \phpcms\modules\member\index.php 用户登陆SQL注入漏洞分析

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述2. 漏洞触发条件 0x1: POC http://localhost/p ...

  8. PHPCMS \phpsso_server\phpcms\modules\phpsso\index.php、\api\get_menu.php Authkey Leakage

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 安装phpcms的时候会强制安装它的通行证 Relevant Link: ...

  9. ecshop /includes/modules/payment/alipay.php SQL Injection Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 ECSHOP支付插件存在SQL注入漏洞,此漏洞存在于/includes/ ...

  10. TypeScript Modules(模块)

    本文概述了TypeScript中如何使用模块以各种方式来组织代码.我们将涵括内部和外部的模块,并且讨论他们在适合在何时使用和怎么使用.我们也会学习一些如何使用外部模块的高级技巧,并且解决一些当我们使用 ...

随机推荐

  1. ARM-Linux中断系统【转】

    转自:https://www.cnblogs.com/arnoldlu/p/7406441.html 1.前言 了解Linux中断子系统,同时也需要了解ARM体系结构中断处理流程:在熟悉整个软硬件架构 ...

  2. go语言设计模式之visitor

    这个确实没有调通,也要记录一下 visitor.go package visitor import ( "fmt" "io" "os" ) ...

  3. go语言设计模式之adapter

    adapter.go package adapter import ( "fmt" ) type LegacyPrinter interface { Print(s string) ...

  4. Python3——根据m3u8下载视频(上)之urllib.request

    干活干活,区区懒癌已经阻挡不了澎湃的洪荒之力了...... 运行环境:Windows基于python3.6 ---------------------------------------------- ...

  5. 史上最全的CSP2019复习指南

    CSP2019复习指南 知识点(大纲)内容参考于本人博客: 近22年NOIP考点一览 算法 基本算法: 模拟.暴力枚举.排序.贪心.递归.递推.贪心.二分.位运算 这些算法不再在此加以赘述,如有考前还 ...

  6. Go package: strings

    Go strings Go 的 strings 包中包含许多处理字符串的函数 官方文档:https://golang.org/pkg/strings/ 前缀.后缀 判断字符串前缀.后缀 // 判断字符 ...

  7. Python:程序练习题(二)

    Python:程序练习题(二) 2.1温度转换程序. 代码如下: t=input("请输入带符号的温度值(如:32C):") if t[-1] in ["C", ...

  8. 【AtCoder】AtCoder Grand Contest 039 解题报告

    点此进入比赛 \(A\):Connection and Disconnection(点此看题面) 大致题意: 给你一个字符串,将它重复\(k\)次.进行尽量少的操作,每次修改一个位置上的字符,使得不存 ...

  9. Python程序中的线程操作-concurrent模块

    目录 一.Python标准模块--concurrent.futures 二.介绍 三.基本方法 四.ProcessPoolExecutor 五.ThreadPoolExecutor 六.map的用法 ...

  10. 时间time()和$_SERVER['REQUEST_TIME']

    文件ab1.php <?phpforeach($i=0;$i<1000;$i++){ echo $time();} 结果: 文件ab2.php <?php foreach($i=0; ...