074——VUE中vuex之模块化modules开发实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vuex之模块化modules开发实例</title>
<script src="vue.js"></script>
<script src="vuex.js"></script>
<script src="node_modules/axios/dist/axios.js"></script>
</head>
<body>
<div id="demo">
<footer-cart></footer-cart>
<Lists></Lists> </div>
<script type="text/x-template" id="Lists">
<div>
<h1 v-if="goods.length==0">
购物车中没有商品
<a href="">去购物吧</a>
</h1>
<div v-if="goods.length>0">
<table border="1">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>数量</th>
<th>总计</th>
<th>操作</th>
</tr>
<tr v-for="v in goods">
<td>{{v.id}}</td>
<td>{{v.title}}</td>
<td>{{v.price}}</td>
<td> <input type="text" v-model="v.num">
</td>
<td>{{v.totalPrice}}</td>
<td>
<button @click="del(v.id)">删除</button>
</td>
</tr>
</table>
</div>
</div>
</script>
<script type="text/x-template" id="footerCart">
<div v-if="totalPrice>0">
<div>
总计:{{totalPrice}}
</div>
</div>
</script>
<script>
let Lists = {
template: "#Lists",
computed: {
goods() {
//console.log(this.$store.state.cart.goods);
return this.$store.getters['cart/goods'];
}
},
methods: {
del(id) {
this.$store.commit('del', {id})
}
}
}
let footerCart = {
template: "#footerCart",
computed: {
totalPrice() {
return this.$store.getters['cart/totalPrice'];
}
}
}
const cartModule={
namespaced:true,//定义组件的命名空间:
state: {
goods: [{id:12}]
},
getters: {
//获取商品总价:
totalPrice: state => {
let totalPrice = 0;
state.goods.forEach((v) => {
totalPrice += v.num * v.price;
});
return totalPrice;
},
goods(state) {
let goods = state.goods;
goods.forEach((v) => {
v.totalPrice = v.num * v.price;
})
return goods;
}
},
mutations: {
//删除购物车中的商品
del(state, param) {
let k;
for (let i = 0; i < state.goods.length; i++) {
if (state.goods[i].id == param.id) {
k = i;
break;
}
}
state.goods.splice(k, 1);
},
setGoods(state, param) {
state.goods = param.goods;
}
},
actions: {
loadGoods(store) {
axios.get('073.php').then(function (response) {
store.commit('setGoods', {goods: response.data})
//console.log(response);
})
}
}
};
let store = new Vuex.Store({
modules:{
cart:cartModule
}
});
var app = new Vue({
el: "#demo",
store,
components: {
Lists, footerCart
},
mounted() {
this.$store.dispatch('cart/loadGoods');
}
});
</script>
</body>
</html>
074——VUE中vuex之模块化modules开发实例的更多相关文章
- vue中vuex的五个属性和基本用法
VueX 是一个专门为 Vue.js 应用设计的状态管理构架,统一管理和维护各个vue组件的可变化状态(你可以理解成 vue 组件里的某些 data ). Vuex有五个核心概念: state, ge ...
- Vue中Vuex的详解与使用(简洁易懂的入门小实例)
怎么安装 Vuex 我就不介绍了,官网上有 就是 npm install xxx 之类的.(其实就是懒~~~哈哈) 那么现在就开始正文部分了 众所周知 Vuex 是什么呢?是用来干嘛的呢? Vuex ...
- vue中scoped vs css modules
注意:此文是默认你已经具备scoped和css modules的相关基础知识,所以不做用法上的讲解. 在vue中,我们有两种方式可以定义css作用域,一种是scoped,另一种就是css module ...
- VUE中toast的使用与开发
在这篇文章中介绍了toast是什么,这篇文章主要介绍toast的开发与使用. 开发 Vuejs很流行,并且官方也给出了路由插件vue-router.数据管理插件vuex,但是我们仅仅停留在了使用的阶段 ...
- 073——VUE中vuex之使用actions和axios异步初始购物车数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 072——VUE中vuex之使用mutations修改购物车仓库数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 071——VUE中vuex之使用getters计算每一件购物车中商品的总价
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 070——VUE中vuex之使用getters计算每一件购物车中商品的总价
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 069——VUE中vuex之使用getters高效获取购物车商品总价
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- 解决Type safety: The expression of type List needs
解决Type safety: The expression of type List needs unchecked conversion to conform to 在方法前加上这句话就可以了@Su ...
- Linq 对List的一些操作
代码: public class Person { public int ID { get; set; } public string Name { get; set; } public int Ag ...
- 【Maven】2.使用Nexus3搭建Maven私服+上传第三方jar包到本地maven仓库
参考文章: http://www.cnblogs.com/luotaoyeah/p/3791966.html --------------------------------------------- ...
- MapReduce程序(一)——wordCount
写在前面:WordCount的功能是统计输入文件中每个单词出现的次数.基本解决思路就是将文本内容切分成单词,将其中相同的单词聚集在一起,统计其数量作为该单词的出现次数输出. 1.MapReduce之w ...
- basename、dirname、alias、date
basename 此命令用于打印目录或者文件的基本名称. basename和dirname命令通常用于shell脚本中的命令替换来指定和指定的输入文件名称有所差异的输出文件名称. basename ( ...
- JS post 数组道后台
$("#aSave").click(function () { if ($("#TaskName").val() == "") { aler ...
- 在SSM框架中,multfile转file
import org.apache.commons.fileupload.disk.DiskFileItem; import org.springframework.web.multipart.Mul ...
- Linux下wget下载整个FTP目录(含子目录)--转载
wget -nH -m --ftp-user=your_username --ftp-password=your_password ftp://your_ftp_host/* 解释:-nH:不创建以主 ...
- 用python生成器实现杨辉三角
先看杨辉三角的形态: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 上学的时候大多是用c语言的两层for循环在实现,现在我们尝试用生成器来实现. 先说思路:我 ...
- Python day21模块介绍4(logging模块,configparser模块)
1.日志等级从上往下依次降低 logging.basicConfig(#日志报错打印的基础配置 level=logging.DEBUG, filename="logger.log" ...