1.计算属性和侦听器
<!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="../node_modules/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ul>
<li>西游记;价格:{{xyPrice}},数量:<input type="number" v-model="xyNum"></li>
<li>水浒传;价格:{{shPrice}},数量:<input type="number" v-model="shNum"></li>
<li>总价:{{totalPrice}}</li>
{{msg}}
</ul>
</div>
<script>
new Vue({
el:"#app",
data:{
xyPrice:81,
shPrice:108,
xyNum:1,
shNum:1,
msg:""
},
computed: {
totalPrice(){
return this.xyPrice*this.xyNum+this.shPrice*this.shNum;
}
},
watch:{
xyNum(newVal,oldVal){
if(newVal>=3){
this.msg="数量超过3";
this.xyNum=3;
}else{
this.msg="";
}
}
}
})
</script>
</body>
</html>
2.过滤器
<!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="../node_modules/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="user in userList">
{{user.id}}===>{{user.name}}==>{{user.gender==0?'女':'男'}}==>
{{user.gender | genderFilter}}==>{{user.gender | gFilter}}
</li>
</ul>
</div>
<script>
Vue.filter("gFilter", function (val) {
if (val == 1) {
return "男~~~~";
} else {
return "女~~~~";
}
});
new Vue({
el: "#app",
data: {
userList: [
{ id: 1, name: "夏琳", gender: 0 },
{ id: 1, name: "尹浩", gender: 1 }
]
},
filters: {
genderFilter(val) {
if (val == 1) {
return "男";
} else {
return "女";
}
}
}
})
</script>
</body>
</html>