vue2.0數(shù)據(jù)雙向綁定與表單bootstrap+vue組件
最近一直在用vue,覺得確實是好用。
一,拿數(shù)據(jù)的雙向綁定來說吧
<!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 是我可以檢測某個數(shù)據(jù)的變化。
v-model=“name”
就是與data中的name數(shù)據(jù)綁定,input框中的值變,那么data中的name也會變,我們可以通過差值操作,也就是{{name}}來看到變化,當然也可以像我一樣打log。都是可以的。
當然這樣也許還不是很實用,官網(wǎng)上也是這么介紹的,那么就說我在工作中是怎么用的吧
現(xiàn)在我的需求是要得到我表單里邊的所有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 };
看到?jīng)],我就是直接用的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>--> <!--發(fā)現(xiàn)這個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數(shù)據(jù)雙向綁定與表單bootstrap+vue組件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- vue使用Element組件時v-for循環(huán)里的表單項驗證方法
- Vue2.0表單校驗組件vee-validate的使用詳解
- vue組件表單數(shù)據(jù)回顯驗證及提交的實例代碼
- Vue表單類的父子組件數(shù)據(jù)傳遞示例
- 詳解vue表單驗證組件 v-verify-plugin
- Vue form表單動態(tài)添加組件實戰(zhàn)案例
- vue動態(tài)綁定組件子父組件多表單驗證功能的實現(xiàn)代碼
- 使用form-create動態(tài)生成vue自定義組件和嵌套表單組件
- 利用Vue v-model實現(xiàn)一個自定義的表單組件
- vue懸浮表單復合組件開發(fā)詳解
相關文章
Vue?3?表格時間監(jiān)控與動態(tài)后端請求觸發(fā)詳解?附Demo展示
在Vue3中,使用el-table組件渲染表格數(shù)據(jù),通過el-table-column指定內容,時間點需前端校準,用getTime()比較,到達時觸發(fā)操作,異步API請求可用async/await處理,setInterval實現(xiàn)定時監(jiān)控,配合條件判斷防止重復請求2024-09-09vue-cli 環(huán)境變量 process.env的使用及說明
這篇文章主要介紹了vue-cli 環(huán)境變量 process.env的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12