购物车Vue组件化开发,
1.创建模板,拆分组件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.contain{
width: 200px;
height: 400px;
margin: 0 auto;
background-color: #FFFFFF;
}
.header{
text-align: center;
width: 200px;
height: 30px;
background-color: aqua;
}
.list div{
height: 30px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.list div:first-child input{
width: 30px;
height: 0.9375rem;
}
.list div:first-child a{
width: 13px;
height: 20px;
text-decoration: none;
background-color: darkgrey;
margin: 3px;
text-align: center;
}
.list div:last-child a{
width: 13px;
height: 20px;
text-decoration: none;
color: red;
font-size: 17px;
margin: 3px;
text-align: center;
}
.footer{
width: 200px;
height: 30px;
background-color: yellow;
padding: 10px 5px;
}
.footer div{
width: 100px;
height: 30px;
font-size: 8px;
background-color: yellow;
display: flex;
flex-direction: row;
align-items: center;
float: right;
flex-wrap: nowrap;
}
.footer div:first-child{
color: red;
}
.footer div:last-child{
width: 100px;
height: 30px;
display: flex;
color: black;
flex-direction: row;
justify-content: center;
background-color: darkred;
}
</style>
</head>
<body>
<div id="app">
<div class="contain">
<my-cart></my-cart>
</div>
</div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript">
var CartTitle = {
template:`
<div class="header">
<div>我的商品</div>
</div>
`,
}
var CartList = {
template:`
<div class="list">
<div>
<div>
<a href="">-</a>
<input />
<a href="">+</a>
</div>
<div>
<a style="background-color: #FFFFFF;">×</a>
</div>
</div>
<div>
<div>
<a href="">-</a>
<input />
<a href="">+</a>
</div>
<div>
<a>×</a>
</div>
</div>
<div>
<div>
<a href="">-</a>
<input />
<a href="">+</a>
</div>
<div>
<a>×</a>
</div>
</div>
<div>
<div>
<a href="">-</a>
<input />
<a href="">+</a>
</div>
<div>
<a>×</a>
</div>
</div>
<div>
<div>
<a href="" style="color:blue;">-</a>
<input />
<a href="" style="color:blue;">+</a>
</div>
<div>
<a>×</a>
</div>
</div>
</div>
`,
}
var CartTotal = {
template:`
<div class="footer">
<div id="">
<div>总价:123</div>
<div>结算</div>
</div>
</div>
`,
}
Vue.component("my-cart",{
template:`
<div class="contain">
<cart-title></cart-title>
<cart-list></cart-list>
<cart-total></cart-total>
</div>
`,
components:{
'cart-title':CartTitle,
'cart-list':CartList,
'cart-total':CartTotal,
}
});
var vm =new Vue({
el:"#app",
data:{
}
})
</script>
</body>
</html>
子组件向父组件传参,和简单计算属性的使用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.contain{
width: 200px;
height: 400px;
margin: 0 auto;
background-color: #FFFFFF;
}
.header{
text-align: center;
width: 200px;
height: 30px;
background-color: aqua;
}
.list div{
height: 30px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.list div:first-child input{
width: 30px;
height: 0.9375rem;
}
.list div:first-child a{
width: 13px;
height: 20px;
text-decoration: none;
background-color: darkgrey;
margin: 3px;
text-align: center;
}
.list div:last-child a{
width: 13px;
height: 20px;
text-decoration: none;
color: red;
font-size: 17px;
margin: 3px;
text-align: center;
}
.footer{
width: 200px;
height: 30px;
background-color: yellow;
padding: 10px 5px;
}
.footer div{
width: 100px;
height: 30px;
font-size: 8px;
background-color: yellow;
display: flex;
flex-direction: row;
align-items: center;
float: right;
flex-wrap: nowrap;
}
.footer div:first-child{
color: red;
}
.footer div:last-child{
width: 100px;
height: 30px;
display: flex;
color: black;
flex-direction: row;
justify-content: center;
background-color: darkred;
}
</style>
</head>
<body>
<div id="app">
<div class="contain">
<my-cart></my-cart>
</div>
</div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript">
var CartTitle = {
props:['username'],
template:`
<div class="header">
<div>{{username}}的商品</div>
</div>
`,
}
var CartList = {
template:`
<div class="list">
<div>
<div>
<a href="">-</a>
<input />
<a href="">+</a>
</div>
<div>
<a style="background-color: #FFFFFF;">×</a>
</div>
</div>
<div>
<div>
<a href="">-</a>
<input />
<a href="">+</a>
</div>
<div>
<a>×</a>
</div>
</div>
<div>
<div>
<a href="">-</a>
<input />
<a href="">+</a>
</div>
<div>
<a>×</a>
</div>
</div>
<div>
<div>
<a href="">-</a>
<input />
<a href="">+</a>
</div>
<div>
<a>×</a>
</div>
</div>
<div>
<div>
<a href="" style="color:blue;">-</a>
<input />
<a href="" style="color:blue;">+</a>
</div>
<div>
<a>×</a>
</div>
</div>
</div>
`,
}
var CartTotal = {
props:['list'],
template:`
<div class="footer">
<div id="">
<div>总价:{{total}}</div>
<div>结算</div>
</div>
</div>
`,
computed:{
total:function(){
//计算商品总价
var t = 0;
this.list.forEach(item => {
t += item.price * item.num;
});
return t;
}
}
}
Vue.component("my-cart",{
data:function(){
return{
username:'张三',
list:[{
id:1,
name:'TCL彩电',
price:'1000',
num:1,
},
{
id:2,
name:'机顶盒',
price:'1000',
num:1,
},
{
id:3,
name:'海尔冰箱',
price:'1000',
num:1,
},
{
id:4,
name:'小米手机',
price:'1000',
num:1,
},
{
id:5,
name:'PPTV电视',
price:'1000',
num:2,
},
]
}
},
template:`
<div class="contain">
<cart-title :username='username'></cart-title>
<cart-list></cart-list>
<cart-total :list='list'></cart-total>
</div>
`,
components:{
'cart-title':CartTitle,
'cart-list':CartList,
'cart-total':CartTotal,
}
});
var vm =new Vue({
el:"#app",
data:{
}
})
</script>
</body>
</html>
删除子组件的一条数据,自定义事件,把id传递给父组件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.contain{
width: 200px;
height: 400px;
margin: 0 auto;
background-color: #FFFFFF;
}
.header{
text-align: center;
width: 200px;
height: 30px;
background-color: aqua;
}
.list div{
height: 30px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.list div:first-child input{
width: 30px;
height: 0.9375rem;
}
.list div:first-child a{
width: 13px;
height: 20px;
text-decoration: none;
background-color: darkgrey;
margin: 3px;
text-align: center;
}
.list div:last-child a{
width: 13px;
height: 20px;
text-decoration: none;
color: red;
font-size: 17px;
margin: 3px;
text-align: center;
}
.footer{
width: 200px;
height: 30px;
background-color: yellow;
padding: 10px 5px;
}
.footer div{
width: 100px;
height: 30px;
font-size: 8px;
background-color: yellow;
display: flex;
flex-direction: row;
align-items: center;
float: right;
flex-wrap: nowrap;
}
.footer div:first-child{
color: red;
}
.footer div:last-child{
width: 100px;
height: 30px;
display: flex;
color: black;
flex-direction: row;
justify-content: center;
background-color: darkred;
}
</style>
</head>
<body>
<div id="app">
<div class="contain">
<my-cart></my-cart>
</div>
</div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript">
var CartTitle = {
props: ['username'],
template: `
<div class="header">
<div>{{username}}的商品</div>
</div>
`,
}
var CartList = {
props: ['list'],
template: `
<div>
<div :key=item.id v-for='item in list' class="list">
<div>
<div>
<a href="">-</a>
<input />
<a href="">+</a>
</div>
<div>{{item.name}}<div>
<div>
<a style="background-color: #FFFFFF;" @click="del(item.id)">×</a>
</div>
</div>
</div>
</div>
`,
methods:{
del:function(id){
//自定义事件,把id传递给父组件
this.$emit('cart-del',id);
}
}
}
var CartTotal = {
props: ['list'],
template: `
<div class="footer">
<div id="">
<div>总价:{{total}}</div>
<div>结算</div>
</div>
</div>
`,
computed: {
total: function() {
//计算商品总价
var t = 0;
this.list.forEach(item => {
t += item.price * item.num;
});
return t;
}
}
}
Vue.component("my-cart", {
data: function() {
return {
username: '张三',
list: [{
id: 1,
name: 'TCL彩电',
price: '1000',
num: 1,
},
{
id: 2,
name: '机顶盒',
price: '1000',
num: 1,
},
{
id: 3,
name: '海尔冰箱',
price: '1000',
num: 1,
},
{
id: 4,
name: '小米手机',
price: '1000',
num: 1,
},
{
id: 5,
name: 'PPTV电视',
price: '1000',
num: 2,
},
]
}
},
template: `
<div class="contain">
<cart-title :username='username'></cart-title>
<cart-list :list='list' @cart-del='delCart($event)'></cart-list>
<cart-total :list='list'></cart-total>
</div>
`,
components: {
'cart-title': CartTitle,
'cart-list': CartList,
'cart-total': CartTotal,
},
methods:{
delCart:function(id){
//根据id删除 list中对应的数据
//1.找到id所对应数据的索引
var index = this.list.findIndex(item=>{
return item.id == id;
});
//2.根据id删除对应数据
this.list.splice(index,1);
}
}
});
var vm = new Vue({
el: "#app",
data: {
}
})
</script>
</body>
</html>
购物车数量变更
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.contain{
width: 200px;
height: 400px;
margin: 0 auto;
background-color: #FFFFFF;
}
.header{
text-align: center;
width: 200px;
height: 30px;
background-color: aqua;
}
.list div{
height: 30px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.list div:first-child input{
width: 30px;
height: 0.9375rem;
}
.list div:first-child a{
width: 13px;
height: 20px;
text-decoration: none;
background-color: darkgrey;
margin: 3px;
text-align: center;
}
.list div:last-child a{
width: 13px;
height: 20px;
text-decoration: none;
color: red;
font-size: 17px;
margin: 3px;
text-align: center;
}
.list div:last-child a:hover{
background-color: #FFFF00;
}
.footer{
width: 200px;
height: 30px;
background-color: yellow;
padding: 10px 5px;
}
.footer div{
width: 100px;
height: 30px;
font-size: 8px;
background-color: yellow;
display: flex;
flex-direction: row;
align-items: center;
float: right;
flex-wrap: nowrap;
}
.footer div:first-child{
color: red;
}
.footer div:last-child{
width: 100px;
height: 30px;
display: flex;
color: black;
flex-direction: row;
justify-content: center;
background-color: darkred;
}
</style>
</head>
<body>
<div id="app">
<div class="contain">
<my-cart></my-cart>
</div>
</div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript">
var CartTitle = {
props: ['username'],
template: `
<div class="header">
<div>{{username}}的商品</div>
</div>
`,
}
var CartList = {
props: ['list'],
template: `
<div>
<div :key=item.id v-for='item in list' class="list">
<div>
<div>
<a href="" @click.prevent='sub(item.id)'>-</a>
<input type="text" :value='item.num' @blur='changeNum(item.id,$event)'/>
<a href="" @click.prevent='add(item.id)'>+</a>
</div>
<div>{{item.name}}<div>
<div>
<a style="background-color: #FFFFFF;" @click="del(item.id)">×</a>
</div>
</div>
</div>
</div>
`,
methods: {
changeNum: function(id) {
// console.log(id,event.target.value);
this.$emit('change-num', {
id: id,
type: 'change',
num: event.target.value
});
},
sub: function(id) {
this.$emit('change-num', {
id: id,
type: 'sub'
});
},
add: function(id) {
this.$emit('change-num', {
id: id,
type: 'add'
});
},
del: function(id) {
//自定义事件,把id传递给父组件
this.$emit('cart-del',{
id: id,
type: 'change'
});
}
}
}
var CartTotal = {
props: ['list'],
template: `
<div class="footer">
<div id="">
<div>总价:{{total}}</div>
<div>结算</div>
</div>
</div>
`,
computed: {
total: function() {
//计算商品总价
var t = 0;
this.list.forEach(item => {
t += item.price * item.num;
});
return t;
}
}
}
Vue.component("my-cart", {
data: function() {
return {
username: '张三',
list: [{
id: 1,
name: 'TCL彩电',
price: '1000',
num: 1,
},
{
id: 2,
name: '机顶盒',
price: '1000',
num: 1,
},
{
id: 3,
name: '海尔冰箱',
price: '1000',
num: 1,
},
{
id: 4,
name: '小米手机',
price: '1000',
num: 1,
},
{
id: 5,
name: 'PPTV电视',
price: '1000',
num: 2,
},
]
}
},
template: `
<div class="contain">
<cart-title :username='username'></cart-title>
<cart-list :list='list' @change-num='changeNum($event)' @cart-del='delCart($event)'></cart-list>
<cart-total :list='list'></cart-total>
</div>
`,
components: {
'cart-title': CartTitle,
'cart-list': CartList,
'cart-total': CartTotal,
},
methods: {
changeNum: function(val) {
// console.log(val);
//分为三种情况:输入域变更,加号变更,减号变更
if (val.type == 'change') {
//根据子组件传递过来的数据更新list中对应数据
console.log('改变值');
this.list.some(item => {
if (item.id == val.id) {
item.num = val.num;
//终止遍历
return true;
}
});
} else if (val.type == 'sub') {
console.log('减少值');
this.list.some(item => {
if (item.id == val.id) {
item.num -= 1;
//终止遍历
return true;
}
});
} else if (val.type == 'add'){
console.log('增加值');
this.list.some(item => {
if (item.id == val.id) {
item.num += 1;
//终止遍历
return true;
}
});
}
},
delCart: function(id) {
//根据id删除 list中对应的数据
//1.找到id所对应数据的索引
var index = this.list.findIndex(item => {
return item.id == id;
});
//2.根据id删除对应数据
this.list.splice(index, 1);
}
}
});
var vm = new Vue({
el: "#app",
data: {
}
})
</script>
</body>
</html>