vuex的购物车效果 index.js
import Vue from 'vue';
import Vuex, {
Store
} from 'vuex';
import { stat } from 'fs'; Vue.use(Vuex);
let store = new Store({
state:{
list: [
{ id:1,
count: 0,
price: 10,
goodsname: '橘子'
},
{ id:2,
count: 0,
price:8,
goodsname: '苹果'
},
{ id:3,
count: 0,
price:4,
goodsname: '香蕉'
},
{ id:4,
count: 0,
price:4,
goodsname: '桃子'
}
],
sum:0,
price:0 },
getters: {
sellPrice(state) {
return (ind)=>{ //getters 传参
return state.list[ind].price * state.list[ind].count;
}
},
mycount(state){
state.sum=0; //必须初始化每次的数量
for(let i= 0;i<state.list.length;i++){//遍历总数
let num = state.list[i].count;
state.sum+=num
}
return state.sum;
},
allPrice(state){
state.sum=0;//必须初始化每次的数量确保每次遍历的值都是准确的
state.price=0;//必须初始化每次的价格确保每次遍历的值都是准确的
for(let i= 0;i<state.list.length;i++){ //遍历总数
let num = state.list[i].count;
let price = state.list[i].price;
state.price += num*price;
}
return state.price;
}
},
mutations: {
add(state,ind) { //进行数量的增加
state.list[ind].count++;
},
del(state,ind) { //进行数量的
if (state.list[ind].count === 0) {
state.list[ind].count === 0;
} else {
state.list[ind].count--;
}
}
}
})
export default store;
app.vue
<template>
<div id="app">
<table class="table">
<tr>
<td>商品</td>
<td>价格</td>
<td>数量</td>
<td colspan="3"></td>
</tr>
<tr v-for='(item,ind) in list' :key="ind">
<td> {{item.goodsname}}</td>
<td>{{item.price}}/斤</td>
<td><input type="text" v-model.number="item.count" class="num" ></td>
<td>总价{{sellPrice(ind)}}</td>
<td>
<button @click="add(ind)">add</button>
</td>
<td><button @click="del(ind)">del</button></td>
</tr>
<tr>
<td colspan="2"></td>
<td>
<span>总量{{mycount}}</span>
</td>
<td>
<span>结算{{allPrice}}</span>
</td>
</tr>
</table>
</div>
</template> <script>
import { mapGetters, mapState, mapMutations, mapActions } from "vuex";
export default {
name: "App",
computed: {
...mapGetters(["sellPrice", "mycount", "allPrice"]),
...mapState(["list"])
},
created() {},
methods: {
...mapMutations(["add", "del"])
}
};
</script> <style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
.num {
max-width:100px;
width: 20px;
display: inline-block;
height: auto;
outline: none;
text-align: center;
}
.table {
width: 500px;
overflow: hidden;
background: skyblue;
}
tr,td{
width: 100px;
text-align: center;
border: none;
border: 1px solid black;
}
</style>
vuex的购物车效果 index.js的更多相关文章
- 踩坑记录-nuxt引入vuex报错store/index.js should export a method that returns a Vuex instance.
错误 store/index.js代码如下: import Vue from 'vue'; import Vuex from 'vuex'; import city from './moudle/ci ...
- 用vuex实现购物车功能
效果图 展示目录结构 product组件(纯静态代码) cart组件(纯静态代码) info组件(纯静态代码) 完成以上的三个组件,现在要开始调用这些组件,在App.vue中调用 如果你的姿势正确的话 ...
- vuex中store分文件时候index.js进行文件整合
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); import getters from './getters.js' impo ...
- angularjs购物车效果
用angularjs写了一个购物车效果中. html代码: <div png-app="myAp" ng-controller="conTroll"> ...
- vuex实现购物车功能
购物车功能描述: 1. 点击+,-按钮,“已选:xx件”随之增减 2. checkbox选中时,当前项的已选数量增加到头部“已选择xx件”中,未选中时数量不计入 代码: 服务端 node+koa+ko ...
- jQuery实现购物车效果
简单的购物车效果 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- JavaScript实现的购物车效果-效果好友列表
JavaScript实现的购物车效果.当然,可以在许多地方使用这种效果.朋友的.例如,在选择.人力资源模块,工资的计算,人才选拔等..下面来看一下班似有些车效果图: watermark/2/text/ ...
- PC,移动端H5实现实现小球加入购物车效果
HTML部分: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" ...
- 使用session页面控制登录入口及购物车效果的实现
由于 Session 是以文本文件形式存储在服务器端的,所以不怕客户端修改 Session 内容.实际上在服务器端的 Session 文件,PHP 自动修改 Session 文件的权限,只 ...
随机推荐
- ztree插件异步加载 使用RESTEasy报错 Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.
在使用ztree插件实现异步加载时遇到后台RESTEasy接收参数问题,查看后台报错: A servlet request to the URI http://localhost:8080/area/ ...
- Call to localhost/127.0.0.1:9000 failed on connection exception:java.net.ConnectException的解决方案
Call to localhost/127.0.0.1:9000 failed on connection exception:java.net.ConnectException的解决方案 作者:凯鲁 ...
- Java(使用JNA)调用DLL库与C#调用DLL库的对比
前言:在项目中经常使用DLL库对硬件进行操作,在发卡过程中使用频率尤为多,今天就Java与C#中调用DLL库的使用区别做一个介绍,本文着重具体的代码编写,具体过程看以下代码. 前提条件: 笔者已经封装 ...
- Ubuntu16.04 下 hadoop的安装与配置(伪分布式环境)
一.准备 1.1创建hadoop用户 $ sudo useradd -m hadoop -s /bin/bash #创建hadoop用户,并使用/bin/bash作为shell $ sudo pass ...
- Linux shell判断文件和文件夹是否存在(转发)
#!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...
- Linux:Day6(下) vim编辑器
vim编辑器 简介: vi:Visual Interface,文本编辑器 文本:ASCII,Unicode 文本编辑种类: 行编辑器:sed 全屏编辑器:nano,vi VIM - Vi IMprov ...
- Python:Day18 os模块、logging模块、正则表达式
迭代器 满足迭代器协议: 1.内部有next方法 2.内部有iter()方法 os模块 import os print(os.getcwd()) #C:\Users\Lowry\PycharmProj ...
- 【vue】vue +element 搭建及开发中项目中,遇到的错误提示
1. import Layout from '@/views/layout/Layout'; export default [ { // 配置路由,当路径为'/activePublic',使用组件ac ...
- emqtt在centos6下的安装
1 emqtt下载地址 http://www.emqtt.com/downloads 右键 复制链接 http://www.emqtt.com/downloads/3011/centos6 2 打开服 ...
- memcache讲解和在.net中初使用
memcache讲解和在.net中初使用 2017年10月17日 22:51:36 等待临界 阅读数:503 前言 传统数据库面临的问题 数据库死锁 磁盘IO 正文 了解memcache 原理 基 ...