vue2.0數(shù)據(jù)雙向綁定與表單bootstrap+vue組件
最近一直在用vue,覺(jué)得確實(shí)是好用。
一,拿數(shù)據(jù)的雙向綁定來(lái)說(shuō)吧
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo1</title>
</head>
<body>
<div id="app">
{{ name }}
<input type="text" v-model="name">
</div>
</body>
<script type="text/javascript" src="vue.js"></script>
<script>
new Vue({
el: '#app',
data: {
name: ''
},
watch: {
name: function () {
console.log(this.name);
}
}
});
</script>
</html>

vue中的所有數(shù)據(jù)都是在data中定義的,
el是指的掛載的元素,
watch 是我可以檢測(cè)某個(gè)數(shù)據(jù)的變化。
v-model=“name” 就是與data中的name數(shù)據(jù)綁定,input框中的值變,那么data中的name也會(huì)變,我們可以通過(guò)差值操作,也就是{{name}}來(lái)看到變化,當(dāng)然也可以像我一樣打log。都是可以的。
當(dāng)然這樣也許還不是很實(shí)用,官網(wǎng)上也是這么介紹的,那么就說(shuō)我在工作中是怎么用的吧

現(xiàn)在我的需求是要得到我表單里邊的所有value ,我們也許可以
let service = $('.vendor').val();
let vendor = document.getElementsByClassName('vendor')[0].value;
但是這樣就完全沒(méi)有g(shù)et到vue雙向綁定的好處了,那么我們?cè)撛趺醋瞿兀?br />
import service from './components/service.vue';
import $ from 'jquery';
export default {
data () {
return {
resultData: '',
vendor: '',
dType: '',
services: [service],
items: [service],
device: '',
dDesc: ''
}
},
watch: {
services () {
console.log(this.services);
},
items (val) {
this.items = val;
console.log(this.items);
}
},
components: {
service
},
methods: {
addService (component) {
this.items.push(component);
},
childServicesChange (val) {
this.services = val;
},
commit () {
console.log('commit');
let device = {
"type": 'urn:' + this.vendor + ':device:' + this.dType + ':0000',
"description": this.dDesc,
"services": this.items
};
看到?jīng)],我就是直接用的this.vendor, vendor是在data中定義好的,也進(jìn)行了雙向綁定v-model
<template> <div class="devDesc">
Device Description
<form class="form-horizontal" role="form" ref="form" id="form">
<div class="form-group">
<label for="vendor" class="col-sm-2 control-label text-left">vendor:</label>
<div class="col-sm-2">
<input type="text" class="form-control vendor" id="vendor" v-model="vendor" control-label name="vendor">
</div>
</div>
<div class="form-group">
<label for="dType" class="col-sm-2 control-label text-left">Type:</label>
<div class="col-sm-2">
<input type="text" class="form-control dType" id="dType" v-model="dType" control-label name="dType">
</div>
</div>
<div class="form-group">
<label for="dDesc" class="col-sm-2 control-label text-left">description:</label>
<div class="col-sm-2">
<input type="text" class="form-control dDesc" id="dDesc" v-model="dDesc" control-label name="dDesc">
</div>
</div>
<!--<serList class="serListPad" :services="services" @services-change="servicesChange">-->
<!--</serList>-->
<!--發(fā)現(xiàn)這個(gè)serList不用抽出來(lái)組件-->
<div class="serList serListPad">
<section class="serList-section">
<span class="span-serList">service List</span>
<button type="button" class="btn btn-default btn-sm" @click="addService(service)">
<span class="glyphicon glyphicon-plus"></span>
</button>
</section>
<!--<service v-for="item in items" :items="items" :myService="myService" @child-services-change="childServicesChange"></service>-->
<div v-for="service in services">
<service v-for="item in items" :items="items" :service="service" @child-services-change="childServicesChange"></service>
</div>
</div>
</form>
<button class="btn btn-info" @click="commit">commit</button>
<button class="btn btn-success">save</button>
</div>
</template>

以上所述是小編給大家介紹的vue2.0數(shù)據(jù)雙向綁定與表單bootstrap+vue組件,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- vue使用Element組件時(shí)v-for循環(huán)里的表單項(xiàng)驗(yàn)證方法
- Vue2.0表單校驗(yàn)組件vee-validate的使用詳解
- vue組件表單數(shù)據(jù)回顯驗(yàn)證及提交的實(shí)例代碼
- Vue表單類的父子組件數(shù)據(jù)傳遞示例
- 詳解vue表單驗(yàn)證組件 v-verify-plugin
- Vue form表單動(dòng)態(tài)添加組件實(shí)戰(zhàn)案例
- vue動(dòng)態(tài)綁定組件子父組件多表單驗(yàn)證功能的實(shí)現(xiàn)代碼
- 使用form-create動(dòng)態(tài)生成vue自定義組件和嵌套表單組件
- 利用Vue v-model實(shí)現(xiàn)一個(gè)自定義的表單組件
- vue懸浮表單復(fù)合組件開發(fā)詳解
相關(guān)文章
關(guān)于this.$refs獲取不到dom的可能原因及解決方法
這篇文章主要介紹了關(guān)于this.$refs獲取不到dom的可能原因及解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
VUE?Element修改el-input和el-select長(zhǎng)度的具體步驟
這篇文章主要給大家介紹了關(guān)于VUE?Element修改el-input和el-select長(zhǎng)度的具體步驟,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
vue3通過(guò)canvas實(shí)現(xiàn)圖片圈選功能
這篇文章將給大家詳細(xì)介紹了vue3如何通過(guò)canvas實(shí)現(xiàn)圖片圈選功能,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴快來(lái)跟隨小編一起學(xué)習(xí)一下吧2023-12-12
vue 使用localstorage實(shí)現(xiàn)面包屑的操作
這篇文章主要介紹了vue 使用localstorage實(shí)現(xiàn)面包屑的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
vue實(shí)現(xiàn)循環(huán)切換動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)循環(huán)切換動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Vue拿到二進(jìn)制流圖片如何轉(zhuǎn)為正常圖片并顯示
這篇文章主要介紹了Vue拿到二進(jìn)制流圖片如何轉(zhuǎn)為正常圖片并顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Vant Uploader實(shí)現(xiàn)上傳一張或多張圖片組件
這篇文章主要為大家詳細(xì)介紹了Vant Uploader實(shí)現(xiàn)上傳一張或多張圖片組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
使用Vue自定義指令實(shí)現(xiàn)Select組件
這篇文章主要介紹了使用Vue自定義指令實(shí)現(xiàn)Select組件,如果哪位朋友對(duì)vue自定義指令不是多了解的話,此篇文章會(huì)對(duì)你有所幫助的,需要的朋友可以參考下2018-05-05

