1、el
el:主要用来挂载指定的dom
* 支持的挂载的选择器有:
* id 选择器,类选择器,标签选择器
* 注意:
* 虽然支持三种选择器,但是只能挂载一个dom,所以一般选择使用id选择器(id具有唯一性)
* *
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div class="app">
{{msg}}
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el:"div",
data:{
msg:"测试"
}
})
</script>
</html>
2、data
主要是准备数据的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div class="app">
{{msg}}<br/>
{{sex}}<br/>
{{person.name}}<br/>
{{person.age}}<br/>
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el:"div",
data:{
msg:"测试",
name:"ergou",
sex:true,
person:{
name:"gg",
age:11
}
}
})
console.debug(vue.msg);
console.debug(vue.person.name);
</script>
</html>
3、methods
当前vue实例中所有的方法都放在methods中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
{{show1()}}<br/>
{{show2("ccccc")}}<br/>
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el:"#app",
data:{
msg:"测试"
},
methods:{
show1(){
console.debug("ceshi 1")
},
show2(msg){
console.debug("Ceshi2"+msg+this.msg)
}
}
})
</script>
</html>
4、表达式
- vue表达式支持,加减乘除模,三目运算。
- vue中为false的有6 个:false,null,NaN,undefined,0,""。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
{{num1}}+{{num2}} = {{num1+num2}}<br/>
{{num1}}-{{num2}} = {{num1-num2}}<br/>
{{num1}}*{{num2}} = {{num1*num2}}<br/>
{{num1}}/{{num2}} = {{num1/num2}}<br/>
{{num1}}%{{num2}} = {{num1%num2}}<br/>
{{sex?"男":"女"}}
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el:"#app",
data:{
msg:"测试",
num1:11,
num2:22,
sex:true
}
})
</script>
</html>
5、操作字符串
java中字符串String的方法,在vue中也可以使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
{{str}}<br/>
{{str.length}}<br/>
{{str.substring(1,3)}}<br/>
{{str.substr(2,3)}}<br/>
{{str.substring(1,3).toUpperCase()}}<br/>
</div>
</body>
<script type="text/javascript">
/*
* */
var vue = new Vue({
el:"#app",
data:{
str:"abcdefg",
}
})
</script>
</html>
6、v-text和v-html
- v-text作为纯文本展示,标签不会起作用
- v-html作为值,会被浏览器解析
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
<div v-text = "text">
a
</div>
<div v-html = "html">
b
</div>
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el:"#app",
data:{
text:"<h1>v-text作为纯文本展示,标签不会起作用</h1>",
html:"<h2>v-html作为值,会被浏览器解析</h2>"
}
})
</script>
</html>
7、v-for
vue中支持循环的有:
* 整数
* 字符串
* 数组
* 对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
<!--循环数字,如果需要索引,(v,i),直接写v,不展示索引-->
<ul>
<li v-for="(v,i) in num">{{v}}===suoyin {{i}}</li>
</ul>
<hr/>
<!--循环字符串-->
<p v-for="(v,i) in str">{{v}}----索引{{i}}</p>
<hr/>
<!--循环数组-->
<p v-for="(v,i) in arr">{{v}}----索引{{i}}</p>
<!--循环对象-->
<hr/>
<p v-for="(v,k) in person">{{v}}----k:{{k}}</p>
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el: "#app",
data: {
num: 10,
str: "abcde",
arr: ["a", "b", "c", "d"],
person: {
name: "ergou",
age: 22,
sex: true
}
}
})
</script>
</html>
8、v-bind
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
<!--v-bind 主要用来绑定属性的-->
<!--原生写法-->
<img src="images/1.jpg" style="width: 300px;height: 200px">
<!--使用v-bind-->
<img v-bind:src="src" v-bind:width="width" v-bind:height="height">
<!--简洁写法-->
<img :src="src" :width="width" :height="height">
<!--使用v-bind的方式来取图片(简写方式 直接绑定对象)-->
<img v-bind="options">
</div>
</body>
<script type="text/javascript">
/*
* vue中支持循环的有:
* */
var vue = new Vue({
el: "#app",
data: {
src: "images/1.jpg",
width:800,
height:500,
alt:"图片不存在",
options:{
src: "images/1.jpg",
width:800,
height:500,
alt:"图片不存在",
}
}
})
</script>
</html>
9、v-model
v-model 双向绑定
他支持的标签:
<input>
<select>
<textarea>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="username">{{username}}<br/>
<input type="checkbox" v-model="hobbys" value="1">爱好1
<input type="checkbox" v-model="hobbys" value="2">爱好2
<input type="checkbox" v-model="hobbys" value="3">爱好3
<input type="checkbox" v-model="hobbys" value="4">爱好4<br/>
{{hobbys}}
<input type="radio" v-model="sex" value="5">男
<input type="radio" v-model="sex" value="6">女
<br/>{{sex}}
<br/>
<select v-model="country">
<option value="-1">请选择</option>
<option value="zh">中国</option>
<option value="us">美国</option>
<option value="jp">日本</option>
</select><br/>
{{country}}
<br/>
<textarea v-model="intro" rows="5" cols="20"></textarea>{{intro}}
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el: "#app",
data: {
//这里相当于默认值,一旦input输入值改变,上面展示的也会改变
username: "二狗",
hobbys:["1","2","3"],
sex:6,
country:"zh",
intro:"踩踩踩踩踩"
}
})
</script>
</html>
10、v-if
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
<span style="color: red;" v-if="age>60&&age<=100">老年了</span>
<span style="color: goldenrod;" v-else-if="age>30&&age<=60">中年</span>
<span style="color: green;" v-else-if="age>18&&age<=30">青少年了</span>
<span style="color: lightseagreen;" v-else-if="age>=0&&age<=18">儿童</span>
<span style="color: deeppink;" v-else>非正常年龄</span>
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el: "#app",
data: {
age: 60,
}
})
</script>
</html>
10、v-on
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<!--v-on: 语法格式,应该写表达式或者触发的函数-->
<div id="app">
<!--完整写法-->
<input type="button" v-on:click="num++" value="按钮">
{{num}}
<input type="button" @click="show('传参')" value="按钮2">
<hr/>
<input type="button" @mouseover="over" @mouseout="out" value="移进移出">
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el:"#app",
data:{
num:0
},
methods:{
show(msg){
alert("show show show show ..."+msg);
},
over(){
console.debug("悬停触发")
},
out(){
console.debug("移除")
}
}
});
</script>
</html>
10、v-show
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<!--v-show: 控制标签属性 是style="display: block" 还是 style="display: none"-->
<div id="app" >
<div v-show="isShow">哈哈哈哈</div>
<input type="button" @mouseover="show" value="显示和隐藏">
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el:"#app",
data:{
isShow:true
},
methods:{
show(){
this.isShow = !this.isShow;
}
}
});
</script>
</html>