最近一直在用vue,覺得確實是好用。
一,拿數據的雙向綁定來說吧
<!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中的所有數據都是在data中定義的,
el是指的掛載的元素,
watch 是我可以檢測某個數據的變化。
v-model=“name” 就是與data中的name數據綁定,input框中的值變,那么data中的name也會變,我們可以通過差值操作,也就是{{name}}來看到變化,當然也可以像我一樣打log。都是可以的。
當然這樣也許還不是很實用,官網上也是這么介紹的,那么就說我在工作中是怎么用的吧

現在我的需求是要得到我表單里邊的所有value ,我們也許可以
let service = $('.vendor').val();
let vendor = document.getElementsByClassName('vendor')[0].value;
但是這樣就完全沒有get到vue雙向綁定的好處了,那么我們該怎么做呢?
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
};
看到沒,我就是直接用的this.vendor, vendor是在data中定義好的,也進行了雙向綁定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>-->
<!--發現這個serList不用抽出來組件-->
<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數據雙向綁定與表單bootstrap+vue組件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。