Vue常用指令詳解分析
Vue入門
Vue是一個(gè)MVVM(Model / View / ViewModel)的前端框架,相對(duì)于Angular來(lái)說(shuō)簡(jiǎn)單、易學(xué)上手快,近兩年也也別流行,發(fā)展速度較快,已經(jīng)超越Angular了。比較適用于移動(dòng)端,輕量級(jí)的框架,文件小,運(yùn)行速度快。最近,閑來(lái)無(wú)事,所以學(xué)習(xí)一下Vue這個(gè)流行的框架,以備后用。
一、指令
- v-model 多用于表單元素實(shí)現(xiàn)雙向數(shù)據(jù)綁定(同angular中的ng-model)
- v-for 格式: v-for="字段名 in(of) 數(shù)組json" 循環(huán)數(shù)組或json(同angular中的ng-repeat),需要注意從vue2開(kāi)始取消了$index
- v-show 顯示內(nèi)容 (同angular中的ng-show)
- v-hide 隱藏內(nèi)容(同angular中的ng-hide)
- v-if 顯示與隱藏 (dom元素的刪除添加 同angular中的ng-if 默認(rèn)值為false)
- v-else-if 必須和v-if連用
- v-else 必須和v-if連用 不能單獨(dú)使用 否則報(bào)錯(cuò) 模板編譯錯(cuò)誤
- v-bind 動(dòng)態(tài)綁定 作用: 及時(shí)對(duì)頁(yè)面的數(shù)據(jù)進(jìn)行更改
- v-on:click 給標(biāo)簽綁定函數(shù),可以縮寫為@,例如綁定一個(gè)點(diǎn)擊函數(shù) 函數(shù)必須寫在methods里面
- v-text 解析文本
- v-html 解析html標(biāo)簽
- v-bind:class 三種綁定方法 1、對(duì)象型 '{red:isred}' 2、三元型 'isred?"red":"blue"' 3、數(shù)組型 '[{red:"isred"},{blue:"isblue"}]'
- v-once 進(jìn)入頁(yè)面時(shí) 只渲染一次 不在進(jìn)行渲染
- v-cloak 防止閃爍
- v-pre 把標(biāo)簽內(nèi)部的元素原位輸出
二、基本組件屬性
new Vue({
el, // 要綁定的 DOM element
template, // 要解析的模板,可以是 #id, HTML 或某個(gè) DOM element
data, // 要綁定的數(shù)據(jù)
computed, // 依賴于別的數(shù)據(jù)計(jì)算出來(lái)的數(shù)據(jù), name = firstName + lastName
watch, // 監(jiān)聽(tīng)方法, 監(jiān)聽(tīng)到某一數(shù)據(jù)變化時(shí), 需要做的對(duì)應(yīng)操作
methods, // 定義可以在元件或模板內(nèi)使用的方法
})
三、基礎(chǔ)使用
1.html
<div id="app">
<p>{{msg}}</p>
</div>
2.js
var app=new Vue({
el:'#app',//標(biāo)簽的類名、id,用于獲取元素
//以鍵值對(duì)的形式存放用到的數(shù)據(jù)成員
data:{
msg:'顯示的內(nèi)容'
},
//包含要用到的函數(shù)方法
methods:{
}
});
這樣js中msg的內(nèi)容就會(huì)在p標(biāo)簽內(nèi)顯示出來(lái)。
四、實(shí)例
利用bootstrap+vue實(shí)現(xiàn)簡(jiǎn)易留言板的功能,可以增加、刪除,彈出模態(tài)框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>簡(jiǎn)易留言板</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
</style>
<link rel="stylesheet" href="../../node_modules/bootstrap/dist/css/bootstrap.min.css" rel="external nofollow" >
<script src="../../node_modules/jquery/dist/jquery.min.js"></script>
<script src="../../node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="../../node_modules/vue/dist/vue.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#box',
data:{
myData:[],
username:'',
age:'',
nowIndex:-100
},
methods:{
add:function(){
this.myData.push({
name:this.username,
age:this.age
});
this.username='';
this.age='';
},
deleteMsg:function(n){
if(n==-2){
this.myData=[];
}else{
this.myData.splice(n,1);
}
}
}
});
};
</script>
</head>
<body>
<div class="container" id="box">
<form role="form">
<div class="form-group">
<label for="username">用戶名:</label>
<input type="text" id="username" class="form-control" placeholder="輸入用戶名" v-model="username">
</div>
<div class="form-group">
<label for="age">年 齡:</label>
<input type="text" id="age" class="form-control" placeholder="輸入年齡" v-model="age">
</div>
<div class="form-group">
<input type="button" value="添加" class="btn btn-primary" v-on:click="add()">
<input type="reset" value="重置" class="btn btn-danger">
</div>
</form>
<hr>
<table class="table table-bordered table-hover">
<h3 class="h3 text-info text-center">用戶信息表</h3>
<tr class="text-danger">
<th class="text-center">序號(hào)</th>
<th class="text-center">名字</th>
<th class="text-center">年齡</th>
<th class="text-center">操作</th>
</tr>
<tr class="text-center" v-for="(item,index) in myData">
<td>{{index+1}}</td>
<td>{{item.name}}</td>
<td>{{item.age}}</td>
<td>
<button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer" v-on:click="nowIndex=$index">刪除</button>
</td>
</tr>
<tr v-show="myData.length!=0">
<td colspan="4" class="text-right">
<button class="btn btn-danger btn-sm" v-on:click="nowIndex=-2" data-toggle="modal" data-target="#layer" >刪除全部</button>
</td>
</tr>
<tr v-show="myData.length==0">
<td colspan="4" class="text-center text-muted">
<p>暫無(wú)數(shù)據(jù)....</p>
</td>
</tr>
</table>
<!--模態(tài)框 彈出框-->
<div role="dialog" class="modal fade bs-example-modal-sm" id="layer">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">確認(rèn)刪除么?</h4>
<button type="button" class="close" data-dismiss="modal">
<span>×</span>
</button>
</div>
<div class="modal-body text-right">
<button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
<button data-dismiss="modal" class="btn btn-danger btn-sm" v-on:click="deleteMsg(nowIndex)">確認(rèn)</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
運(yùn)行效果:

相關(guān)文章
解決iview table組件里的 固定列 表格不自適應(yīng)的問(wèn)題
這篇文章主要介紹了解決iview table組件里的 固定列 表格不自適應(yīng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
vue中element 上傳功能的實(shí)現(xiàn)思路
這篇文章主要介紹了vue中element 的上傳功能的實(shí)現(xiàn)思路,本文大概通過(guò)兩種實(shí)現(xiàn)思路,具體內(nèi)容詳情大家跟隨腳本之家小編一起看看吧2018-07-07
vue2+element-ui使用vue-i18n進(jìn)行國(guó)際化的多語(yǔ)言/國(guó)際化詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于vue2+element-ui使用vue-i18n進(jìn)行國(guó)際化的多語(yǔ)言/國(guó)際化的相關(guān)資料,I18n是Vue.js的國(guó)際化插件,項(xiàng)目里面的中英文等多語(yǔ)言切換會(huì)使用到這個(gè)東西,需要的朋友可以參考下2023-12-12
el-table渲染慢卡頓問(wèn)題最優(yōu)解決方案
本文主要介紹了el-table渲染慢卡頓問(wèn)題最優(yōu)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Vue實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)百分比進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)百分比進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
父子組件生命周期及子組件獲取數(shù)據(jù)傳值問(wèn)題剖析
這篇文章主要介紹了父子組件生命周期及子組件獲取數(shù)據(jù)問(wèn)題剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
vue使用docx-preview實(shí)現(xiàn)docx文件在線預(yù)覽功能全過(guò)程
文件在線預(yù)覽是目前移動(dòng)化辦公的一種新趨勢(shì),下面這篇文章主要給大家介紹了關(guān)于vue?docx-preview實(shí)現(xiàn)docx文件在線預(yù)覽功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04

