<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>demo2</title>
</head>
<body>
<div id="app">
{{msg}}
<button>alert</button>
<div v-if="show">展示if</div>
<div v-else>展示else</div>
<div v-show="vshow">如果v-show的值為false</div>
</div>
<script src="../vue.js"></script>
<script>
new Vue({
el:"#app",
data:{
msg:"hello word 123" , //不操作dom元素 直接修改script中的變量
show:true,
vshow:false
},
methods:{
}
})
</script>
</body>
</html>
v-if v-else
圖片.png
v-show
圖片.png
圖片.png
v-for
<p v-for="(k,v) in arr">第{{k}}個(gè) 是 {{v}} </p>
script是
arr:["ele1","ele2","ele3"]
顯示是:
圖片.png
<!-- 注意先是value 再是key 最后是inde -->
<p v-for="(v,k,index) in obj">obj 中的key是{{k}}蚤假,v是{{v}}</p>
當(dāng)對(duì)象都放到一個(gè)數(shù)組中呢
<p v-for=" value in objs">數(shù)組objs中 name 是{{value.name}},str1是{{value.str1}}</p>
script中
objs:[
{
name:"objName1",
str1:"str11",
str2:"str12"
},{
name:"objName2",
str1:"str21",
str2:"str22"
},{
name:"objName3",
str1:"str31",
str2:"str32"
}
]
顯示是
圖片.png
v-on 綁定事件
<!-- v-on 跟一個(gè)函數(shù) 需要()-->
<button v-on:click="add()">v-on點(diǎn)擊按鈕</button>
<div>點(diǎn)擊后的結(jié)果是{{count}}</div>
腳本中定義防范
//注意需要this.
methods:{
add(){
this.count++
}
}
圖片.png
v-bind 綁定屬性
<!-- v-bind是綁定屬性 :其實(shí)是v-bind:簡(jiǎn)寫的方式-->
<div style="width: 100px; height: 74px;border: 1px solid #100" :style="bgColor"></div>
腳本中是
bgColor:{
backgroundColor:'red'
}
顯示
圖片.png
v-model
<!-- v-model就是綁定數(shù)據(jù) -->
<input type="text" v-model="text">
<button @click="showText()"> 點(diǎn)擊打印text</button>
腳本中data添加
text:'123'
},
methods:{
add(){
this.count++
},
showText(){
console.log(this.text)
}
顯示的結(jié)果是
圖片.png