<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
.list {
margin: 10px;
display: flex;
}
.list li {
color: burlywood;
border: 1px solid burlywood;
margin: 0 10px;
padding: 5px 10px;
cursor: pointer;
}
.list li.active {
color: chocolate;
border: 1px solid chocolate;
}
input {
width: 50px;
text-align: center;
height: 20px;
}
button {
width: 20px;
height: 25px;
font-size: 20px;
text-align: center;
}
[v-cloak]{
display: none;
}
</style>
</head>
<body>
<div id="app" v-cloak>
<ul class="list">
<li @click="activeIndex=i" v-for="(item, i) in list" :class='{active:i===activeIndex}'>
{{item.name}}
</li>
</ul>
<div style="display: flex; margin-left: 20px;">
<button :disabled="count===1" @click="count--">-</button>
<input type="text" v-model="count">
<button :disabled="count===6" @click="count++">+</button>
</div>
<!-- <h1>
{{count*list[activeIndex].price}}
</h1> -->
<!-- <h1>
{{totalPrice}}
</h1> -->
<h1>
{{totalPrice | fmtPrice}}
</h1>
<h1>
{{totalPriceUS | fmtUSPrice}}
</h1>
</div>
<script>
let vm = new Vue({
el: "#app",
filters:{
fmtPrice(val){
return '¥'+val.toFixed(2)
},
fmtUSPrice(val){
return '$'+val.toFixed(2)
}
},
watch: {
count(val){
if(isNaN(val)) val=1
if(!Number.isInteger(val)) val=parseInt(val)
if(val<1) val=1
if(val>6) val=6
this.count=val
}
},
computed: {
totalPrice(){
return this.count*this.list[this.activeIndex].price
},
totalPriceUS(){
return this.totalPrice/6.9
}
},
data() {
return {
activeIndex: 0,
count: 1,
list: [
{
name:'1.5磅',
price:100
},
{
name:'2磅',
price:200
},
{
name:'2.5磅',
price:300
},
{
name:'3磅',
price:400
},
]
}
},
methods: {
},
})
</script>
</body>
</html>