<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vuex之使用getters计算每一件购物车中商品的总价</title>
<script src="vue.js"></script>
<script src="vuex.js"></script>
</head>
<body>
<div id="demo">
<footer-cart></footer-cart>
<Lists></Lists> </div>
<script type="text/x-template" id="Lists">
<div>
<h1>购物车</h1>
<table border="1">
<tr>
<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>
</tr>
</table> </div>
</script>
<script type="text/x-template" id="footerCart">
<div>
总计:{{totalPrice}}
</div>
</script>
<script>
let Lists = {
template: "#Lists",
computed: {
goods() {
return this.$store.getters.goods;
}
}
}
let footerCart = {
template: "#footerCart",
computed: {
totalPrice() {
return this.$store.getters.totalPrice;
}
}
}
let store = new Vuex.Store({
state: {
goods: [
{id: 1, title: 'ihpone7', price: 100, num: 3},
{id: 2, title: 'vivo20', price: 100, num: 2}
]
},
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;
}
}
});
var app = new Vue({
el: "#demo",
store,
components: {
Lists,footerCart
}
});
</script>
</body>
</html>

  

最新文章

  1. Oracle学习总结_day01_day02_表的创建_增删改查_约束
  2. MongoDB csv文件导入导出
  3. 利用css3新增选择器制作背景切换
  4. 在Linux中让打印带颜色的字
  5. jQuery coveringBad 效果对比
  6. 13. Android框架和工具之 Android Drawable Factory
  7. 造一个Badge Service(徽章)的轮子
  8. java笔记3之赋值运算符
  9. BZOJ 1176([Balkan2007]Mokia-CDQ分治-分治询问)
  10. POJ 2352 &amp;amp;&amp;amp; HDU 1541 Stars (树状数组)
  11. ENode 2.0
  12. poj 1056 IMMEDIATE DECODABILITY 字典树
  13. c++ 求集合的交并补
  14. 腾讯云centos7安装MySQL
  15. gcc update in centos to 6.3 by scl
  16. iframe可通过postMessage解决跨域、跨窗口消息传递
  17. [NOI 2017]蚯蚓排队
  18. LuoguP2398 GCD SUM
  19. SharePoint 2013 Step by Step——使用自定义的List Template
  20. Mybatis系列(四):Mybatis缓存

热门文章

  1. bzoj 2654 tree - 二分法 - 最小生成树
  2. 简谈高通Trustzone的实现【转】
  3. Linux写时拷贝技术【转】
  4. Notepad++7.5.4 设置主题,使用插件
  5. 怎么在VS监视DataSet类型的数据
  6. Chromosome coordinate systems: 0-based, 1-based
  7. LuoguP3183 [HAOI2016]食物链 记忆化搜索
  8. c#传统SqlTransaction事务和TransactionScope事务
  9. Elasticsearch 原理
  10. Ajax 随笔