<div><divid='app'>{{username}}div><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>//创建vm实例对象(vue实例对象)const vm =newVue({//指定当前vm实例要控制页面的哪个区域el:'#app',//指定Model数据源data:{username:'zs'}})script>div>
<divid='app'><pv-text='username'>p><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{username:'zs'}})script>div>
1
2
3
4
5
6
7
8
9
10
11
12
{{}}—插值表达式
<divid='app'><div>{{username}}div><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{username:'zs'}})script>script>div>
1
2
3
4
5
6
7
8
9
10
11
12
13
v-html
<divid='app'><divv-html='info'>div><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{info:`
<divid='app'><inputtype='input'v-bind:placeholder='tips'>input><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{tips:'请输入用户名'}})script>script>div>
1
2
3
4
5
6
7
8
9
10
11
12
13
事件绑定指令
v-on:click / @click
<divid='app'><p>count的值是:{{count}}p><divv-on:click='add'>+1div><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{count:0},methods:{add:function(){this.count+=1}}})script>script>div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
事件对象$event
<divid='app'><p>count的值是:{{count}}p><divv-on:click='add(1,$event)'>+1div><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{count:0},methods:{add(n,e){this.count += n
}if(this.count %2===0){
e.target.style.backgroundColor ='red'}else{
e.target.style.backgroundColor =''}}})script>script>div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
事件修饰符
<divid='app'><ahref='https://www.baidu.com'@click='show'>报读一下a><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{},methods:{show(e){//原生不跳转页面
e.preventDefault()}}})script>script>div><divid='app'><ahref='https://www.baidu.com'@click.prevent>报读一下a><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{},methods:{}}})script>script>div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
事件修饰符
说明
.pervent
组织默认行为(a标签跳转行为)
.stop
组织事件冒泡
.capture
以捕获模式触发当前的事件处理函数
.once
绑定事件只触发1次
.self
只有在event.target是当前元素自身时触发事件处理函数
其他修饰符与点击修饰符用法一样
双向绑定指令
v-model
<divid='app'><p>用户的名字是: {{username}}p><inputtype='text'v-model='username'>input><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{username:'mz'}})script>script>div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
修饰符
作用
示例
.number
自动将用户的输入值转换为数值类型
条件渲染指令
v-if v-show
<divid='app'><pv-if='msg'>v-if控制p><pv-show='msg'>v-show控制p><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{msg:true}})script>script>div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
v-if v-else-if v-else
<divid='app'><divv-if='type === "A"'>优秀div><divv-else-if='type === "B"'>良好div><divv-else-if='type === "C"'>一般div><divv-else>差div><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{type:'A'}})script>script>div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
列表渲染指令
v-for
<divid='app'><tableclass='table table-bordered table-hover table-striped'><thead><th>索引th><th>IDth><th>姓名th>thead><tbody><trv-for='(item,index) in list':key='item.id'><td>{{index}}td><td>{{item.id}}td><td>{{item.name}}td>tr>tbody>table><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{list:[{id:1,name:'张三'},{id:2,name:'历史'}]}})script>script>div>tr>tbody>table><scriptsrc="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js">script><script>const vm =newVue({el:'#app',data:{list:[{id:1,name:'张三'},{id:2,name:'历史'}]}})script>script>div>