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一個Vue實例,實例化的時候傳入了一個對象{el:'#box',data:{msg:'Hello Vue!'}}。這個意思是:Vue這個只控制id="box"這個DIV元素,同時在 HTML模板上使用雙花括號{{xxxx}}語法,來訪問data中定義的數(shù)據(jù)。
上面代碼我們new處理一個Vue的實例,并賦值給了vm變量,通過這個vm變量,我們也可以訪問其中定義的數(shù)據(jù):
var vm = new Vue({ el:'#box', data:{ msg:'Hello Vue!' } }); console.log(vm.msg); //'Hello Vue!'
2.v-model指令
所謂的“指令”其實就是擴展了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>
通過v-model 指令,我們把msg 數(shù)據(jù)綁定到了input文本框,我們修改文本框的值,發(fā)現(xiàn)msg 數(shù)據(jù)改變了。
注意:如果我們定義的數(shù)據(jù)是數(shù)組或者json,在模板上會怎樣顯示出來呢?
<!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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js實現(xiàn)大轉(zhuǎn)盤抽獎總結(jié)及實現(xiàn)思路
這篇文章主要介紹了 Vue.js實現(xiàn)大轉(zhuǎn)盤抽獎總結(jié)及實現(xiàn)思路,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10Vue OptionsAPI與CompositionAPI的區(qū)別與使用介紹
OptionsAPI和CompositionAPI是Vue.js框架中兩種不同的組件編寫方式,OptionsAPI通過對象字面量定義組件,以屬性分隔不同功能,響應(yīng)式數(shù)據(jù)通過data屬性定義,本文給大家介紹Vue OptionsAPI與CompositionAPI的區(qū)別,感興趣的朋友一起看看吧2024-10-10elementUI el-form 數(shù)據(jù)無法賦值且不報錯解決方法
本文主要介紹了elementUI el-form 數(shù)據(jù)無法賦值且不報錯解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12vue頁面設(shè)置滾動失敗的完美解決方案(scrollTop一直為0)
這篇文章主要介紹了vue頁面設(shè)置滾動失敗的解決方案(scrollTop一直為0),本文通過場景分析實例代碼相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下2023-05-05useEffect理解React、Vue設(shè)計理念的不同
這篇文章主要為大家介紹了useEffect理解React、Vue設(shè)計理念的不同詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09VUE2.0+Element-UI+Echarts封裝的組件實例
下面小編就為大家分享一篇VUE2.0+Element-UI+Echarts封裝的組件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03