一、过滤器
<!-- 过滤器 --> <div id="app"> {{msg | format(3)}} </div> <script> var vm = new Vue({ el:"#app", data:{ msg:"99" }, filters:{ /* val就是msg params就是管道修饰符后面函数传递过来得参数*/ format(val){ return "$"+val.toFixed(params); } } }) </script>
二、计算属性
<div id="app"> 价格:<input type="number" min="1" v-model.number="price"/> 数量:<input type="number" min="1" v-model.number="count"/> 总价:<!-- {{price*count}} -->{{total}} </div> <script> var vm = new Vue({ el:"#app", data:{ price:6, count:5 }, computed:{ total(){ return this.price*this.count; } } }) </script>
三、get set
<title>计算属性得get,set方法</title> <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script></head><body> <div id="app"> <input type="checkbox" v-model="isChecked"> </div> <script> var vm = new Vue({ el:"#app", computed:{ isChecked:{ get(){ return false } } } }) </script>
四、checkbox
<div id="#app"> <input type="checkbox" v-model = "checkState" @click="change"> </div> <script> var vm = new Vue({ el:"#app", data:{ checkState:false }, methods: { change(){ console.log(this.checkState) } }, }) </script>
五、购物车
<style> img{ width:70px; height: 100px; } .number{ width:40px; } </style> <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script> <script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script> <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.css" rel="stylesheet"></head><body> <div id="app" class="container"> <h2 class="text-center text-success">购物车</h2> <table class="table table-bordered table-hover"> <thead> <tr> <th>全选 <input @change="change" type="checkbox" v-model="checkAll"></th> <th>商品</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="item of products"> <td> <input type="checkbox" @change="handleItem" v-model="item.isSelected"> </td> <td> <img :src="item.productCover" alt=""> {{item.productName}} </td> <td> {{item.productPrice}} </td> <td> <input class="number" type="number" v-model.number="item.productCount" min="1"> </td> <td> {{item.productPrice*item.productCount | format(2)}} </td> <td><button class="btn btn-danger" @click="handleDelete">删除</button></td> </tr> </tbody> </table> <p>总价格:{{sum() | format(2)}}</p> </div> <script> /* ctrl+shift+p */ var vm = new Vue({ el:"#app", data:{ products:[], checkAll:false }, mounted(){ axios.get('./data/carts.json').then(res=>{ console.log(res.data) this.products = res.data; }) this.handleItem() }, filters:{ format(val,params){ return "$"+val.toFixed(params) } }, computed:{ checkAll:{ get(){ return this.products.every(item=>item.isSelected) }, set(val){ this.products.forEach(item=>item.isSelected = val) } }, sum(){ alert(1) var total = 0; this.products.forEach(item=>{ total = item.productCount*item.productPrice+total; }) return total } } }) </script></body></html>