v-text/v-html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="../node_modules/vue/dist/vue.js"></script>
<div id="app">
{{msg}} {{hello()}}<br/>
<span v-html="msg"></span>
<br/>
<span v-text="msg"></span>
<a href="">gogogo</a>
</div>
<script>
new Vue({
el: "#app",
data: {
msg: "<h1>Hello,Vue</h1>"
link:"www.baidu.com"
},
methods: {
hello(){
return "hello";
}
}
})
</script>
</body>
</html>
单向绑定v-bind
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="../node_modules/vue/dist/vue.js"></script>
<div id="app">
<a v-bind:href="link">gogogo</a>
<span v-bind:class="{active:isActive,'text-danger':hasError}"
v-bind:style="{color:color1,fontSize: size}"
>你好</span>
</div>
<script>
var vue = new Vue({
el: "#app",
data: {
link: "http://www.baidu.com",
isActive: true,
hasError: true,
color1: 'red',
size: "36px"
},
methods: {
}
})
</script>
</body>
</html>
双向绑定v-model
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="../node_modules/vue/dist/vue.js"></script>
<div id="app">
精通的语言: <br/>
<input type="checkbox" v-model="language" value="Java" /> Java <br/>
<input type="checkbox" v-model="language" value="PHP" /> PHP <br/>
<input type="checkbox" v-model="language" value="Python" /> Python <br/>
选中了 {{language.join(",")}}
</div>
<script>
var vue = new Vue({
el: "#app",
data: {
language: []
},
methods: {
}
})
</script>
</body>
</html>
事件绑定v-on
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="../node_modules/vue/dist/vue.js"></script>
<div id="app">
<button @click="num++">点赞</button>
<button @click="cancel">取消</button>
<h1>有 {{num}}个赞</h1>
<div style="border: 1px solid red;padding: 20px" @click="hello()">
大DIV
<div style="border: 1px solid blue;padding: 20px" @click.stop="hello()">
大DIV <br/>
<a href="http://www.baidu.com" @click.stop>去百度</a>
</div>
</div>
<input type="text" v-model="num" v-on:keyup.up="num+=2" @keyup.down="num-=2" @click.ctrl="num=10" /> <br/>
提示:
</div>
<script>
var vue = new Vue({
el: "#app",
data: {
num: 1
},
methods: {
cancel(){
this.num--;
},
hello(){
alert("被点击了")
}
}
})
</script>
</body>
</html>
遍历循环v-for
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="../node_modules/vue/dist/vue.js"></script>
<div id="app">
<ul>
<li v-for="(item,index) in users" :key="index">
{{index}}: {{item.name}} ==> {{item.gender}} ==> {{item.age}} <br/>
对象信息:
<span v-for="(v,k,i) in item">{{k}} ==> {{v}} ==> {{i}}</span>
</li>
</ul>
<ul>
<li v-for="(num,index) in numbers" :key="index"></li>
</ul>
</div>
<script>
new Vue({
el: "#app",
data: {
users: [
{
name: "iPhone",
gender: "女",
age: "12"
},{
name: "法外狂徒",
gender: "男",
age: "35"
},{
name: "刀锋战士",
gender: "艾瑞莉娅",
age: "25"
},{
name: "影流之主",
gender: "劫",
age: "28"
},{
name: "影流之隐",
gender: "凯隐",
age: "23"
}
],
numbers: [1,2,3,4]
},
methods: {
hello(){
return "hello";
}
}
})
</script>
</body>
</html>
判断指令v-if和v-show
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="../node_modules/vue/dist/vue.js"></script>
<div id="app">
<button @click="show = !show">点我呀</button>
<h1 v-if="show">if=看打我。。。</h1>
<h1 v-if="show">show=看我到</h1>
</div>
<script>
new Vue({
el: "#app",
data: {
show: true
},
methods: {
}
})
</script>
</body>
</html>
计算属性和侦听属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="../node_modules/vue/dist/vue.js"></script>
<div id="app">
<ul>
<li>西游记:价格: {{xyjPrice}} 数量 <input type="number" v-model="xyjNum" /></li>
<li>水浒传:价格: {{shzPrice}} 数量 <input type="number" v-model="shzNum" /></li>
<li>总价:{{totalPrice}}</li>
<li>{{msg}}</li>
</ul>
</div>
<script>
new Vue({
el: "#app",
data: {
xyjPrice: 99.98,
shzPrice: 99.00,
xyjNum: 1,
shzNum: 1,
msg:""
},
methods: {
},
computed:{
totalPrice() {
return this.xyjPrice * this.xyjNum + this.shzPrice * this.shzNum;
}
},
watch:{
xyjNum(newVal,oldVal){
if (newVal >= 3){
this.msg = "库存超出限制";
this.xyjNum = 1;
}else {
this.msg = "";
}
}
}
})
</script>
</body>
</html>
过滤器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="../node_modules/vue/dist/vue.js"></script>
<div id="app">
<ul>
<li v-for="user in userList">
{{user.id}} ==> {{user.name}} ==> {{user.gender == 1 ? "男" : "女"}} ==> {{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: "jack",
gender: 1
},
{
id: 2,
name: "Peter",
gender: 0
}
]
},
methods: {
},
filters: {
genderFilter(val) {
if (val == 1) {
return "男"
} else {
return "女"
}
}
}
})
</script>
</body>
</html>