VUE中v-model和v-for指令詳解
1.基本雛形
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
data:{
msg:'Hello Vue!'
}
});
}
</script>
</head>
<body>
<div id="box">
{{msg}}
</div>
</body>
</html>
需要new一個(gè)Vue實(shí)例,實(shí)例化的時(shí)候傳入了一個(gè)對(duì)象{el:'#box',data:{msg:'Hello Vue!'}}。這個(gè)意思是:Vue這個(gè)只控制id="box"這個(gè)DIV元素,同時(shí)在 HTML模板上使用雙花括號(hào){{xxxx}}語(yǔ)法,來(lái)訪問(wèn)data中定義的數(shù)據(jù)。
上面代碼我們new處理一個(gè)Vue的實(shí)例,并賦值給了vm變量,通過(guò)這個(gè)vm變量,我們也可以訪問(wèn)其中定義的數(shù)據(jù):
var vm = new Vue({
el:'#box',
data:{
msg:'Hello Vue!'
}
});
console.log(vm.msg); //'Hello Vue!'
2.v-model指令
所謂的“指令”其實(shí)就是擴(kuò)展了HTML標(biāo)簽功能(屬性)。
v-model的雙向數(shù)據(jù)綁定
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
data:{
msg:'Hello Vue!'
}
});
}
</script>
</head>
<body>
<div id="box">
<input type="text" v-model="msg"/><br/>
{{msg}}
</div>
</body>
</html>
通過(guò)v-model 指令,我們把msg 數(shù)據(jù)綁定到了input文本框,我們修改文本框的值,發(fā)現(xiàn)msg 數(shù)據(jù)改變了。

注意:如果我們定義的數(shù)據(jù)是數(shù)組或者json,在模板上會(huì)怎樣顯示出來(lái)呢?
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
data:{
msg:'Hello Vue!',
arr:['apple','banana','orange'],
json:{a:'apple',b:'banana'}
}
});
}
</script>
</head>
<body>
<div id="box">
<input type="text" v-model="msg"/><br/>
{{msg}} <br/>
{{arr}} <br/>
{{json}}
</div>
</body>
</html>
數(shù)組和json都被當(dāng)作字符串輸出了,顯然這不是我們理想的效果。
3.v-for指令
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
data:{
arr:['apple','banana','orange'],
json:{a:'apple',b:'banana'}
}
});
}
</script>
</head>
<body>
<div id="box">
<p>循環(huán)數(shù)組</p>
<ul>
<li v-for="a in arr">
{{a}}
</li>
</ul>
<hr>
<p>循環(huán)出數(shù)組索引</p>
<ul>
<li v-for="(v,k) in arr">
{{v}}==>{{k}}
</ul>
<p>循環(huán)json</p>
<ul>
<li v-for="item in json">{{item}}</li>
</ul>
<p>循環(huán)json的鍵</p>
<ul>
<li v-for="(k,v) in json">
{{k}}==>{{v}}
</li>
</ul>
</div>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js實(shí)現(xiàn)大轉(zhuǎn)盤(pán)抽獎(jiǎng)總結(jié)及實(shí)現(xiàn)思路
這篇文章主要介紹了 Vue.js實(shí)現(xiàn)大轉(zhuǎn)盤(pán)抽獎(jiǎng)總結(jié)及實(shí)現(xiàn)思路,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Vue OptionsAPI與CompositionAPI的區(qū)別與使用介紹
OptionsAPI和CompositionAPI是Vue.js框架中兩種不同的組件編寫(xiě)方式,OptionsAPI通過(guò)對(duì)象字面量定義組件,以屬性分隔不同功能,響應(yīng)式數(shù)據(jù)通過(guò)data屬性定義,本文給大家介紹Vue OptionsAPI與CompositionAPI的區(qū)別,感興趣的朋友一起看看吧2024-10-10
elementUI el-form 數(shù)據(jù)無(wú)法賦值且不報(bào)錯(cuò)解決方法
本文主要介紹了elementUI el-form 數(shù)據(jù)無(wú)法賦值且不報(bào)錯(cuò)解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
vue頁(yè)面設(shè)置滾動(dòng)失敗的完美解決方案(scrollTop一直為0)
這篇文章主要介紹了vue頁(yè)面設(shè)置滾動(dòng)失敗的解決方案(scrollTop一直為0),本文通過(guò)場(chǎng)景分析實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
useEffect理解React、Vue設(shè)計(jì)理念的不同
這篇文章主要為大家介紹了useEffect理解React、Vue設(shè)計(jì)理念的不同詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
vue3 $attrs和inheritAttrs的用法說(shuō)明
這篇文章主要介紹了vue3 $attrs和inheritAttrs的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
VUE2.0+Element-UI+Echarts封裝的組件實(shí)例
下面小編就為大家分享一篇VUE2.0+Element-UI+Echarts封裝的組件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue3實(shí)現(xiàn)表格編輯和刪除功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了vue3實(shí)現(xiàn)表格編輯和刪除功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01

