Vue制作简易购物车
Vue 制作 简易 购物车
2023-09-11 14:15:13 时间
功能描述
- 修改商品数量,同时价格动,总价态变化。
- 点击删除按钮可以动态删除商品
具体代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="vue3.0.js"></script> //引入vue.js包
<style>
body {
width: 600px;
}
table {
border: 1px solid black;
}
table {
width: 100%;
}
th {
height: 50px;
}
th, td {
border-bottom: 1px solid #ddd;
text-align: center;
}
span {
float: right;
}
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<div id="app">
<table>
<tr>
<th>商品号</th>
<th>商品</th>
<th>单价</th>
<th>数量</th>
<th>金额</th>
<th>操作</th>
</tr>
<tr v-for='(book,index) in books' :key="book.id">
<td>{{book.id}}</td>
<td>{{book.title}}</td>
<td>{{book.price}}</td>
<td>
<button :disabled="book.count===0" @click="book.count--">-</button>
{{book.count}}
<button @click="book.count++">+</button>
</td>
<td>{{itemprice(book.price,book.count)}}</td>
<td><button @click="deleteitem(index)">删除</button></td>
</tr>
</table>
<p>总价:¥{{totalprice}}</p>
</div>
<script>
//vue3.0语法
const vm=Vue.createApp({
data(){
return{
books:[{
id: 1,
title: 'Java无难事',
price: 188,
count: 1
},
{
id: 2,
title: 'C++深入详解',
price: 168,
count: 1
},
{
id: 3,
title: 'Servlet/JSP深入详解',
price: 139,
count: 1
}]
}
},
methods:{
itemprice(price,count){
return price*count;
},
deleteitem(index){
this.books.splice(index,1);
}
},
computed:{
totalprice(){
let total=0;
for(let book of this.books)
{
total+=book.price*book.count;
}
return total;
}
}
}).mount("#app");
</script>
</body>
</html>
要点注意 ❗
这里注意要用for...of循环
区别for...in和for...of的用法
for-in遍历的是key,只能获得对象的键名,不能获得键值(适合遍历对象)
for-of遍历的是value,允许遍历获得键值(适合遍历数组)
具体效果
相关文章
- Vue - 完美解决小数的四则运算(加减乘除)导致精度丢失问题,提供详细计算示例代码vue数据计算丢失精度
- B站(云e办)SpringBoot实战练习的Sql文件、前端Vue源码、后端springboot源码
- Web前端-Vue.js必备框架(三)
- vant ui 在vue中的安装和使用
- Vue 爬坑之路(一)—— 使用 vue-cli 搭建项目 (增补)
- Vue 国际化之 vue-i18n 的使用
- Vue项目中使用svg图标
- vue学习笔记八:Jquery VS Vue之全局属性对照
- vue学习笔记四:Jquery VS Vue之元素操作明细对照
- 理解Vue的状态管理模式Vuex
- 前端技术:vue(Vue项目中-axios设置默认请求地址和请求头)
- Vue 之 mockjs 结合 axios 在 vue 中的随机数据生成的简单使用
- Django+Vue项目学习第六篇:vue+django发送post请求,设置不同content-type,前后端如何处理参数
- Django+Vue项目学习第三篇:使用axios发送get请求,解决跨域问题,调通前后端
- Django+Vue项目学习第二篇:vue项目创建
- Vue学习第27天——路由vue-router的详解及案例练习
- Vue学习第21天——插槽slot的理解及案例
- Vue学习第20天——Vue中常用的ajax请求库(axios与vue-rouserce)
- 【VUE】vue配置Gzip压缩
- vue-解决Vue打包上线之后部分CSS不生效的问题