最近一直在用vue,覺得確實是好用。
一,拿資料的雙向綁定來說吧
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>demo1</title> </head> <body> <p id="app"> {{ name }} <input type="text" v-model="name"> </p> </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;
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 };
<template> <p class="devDesc">
<form class="form-horizontal" role="form" ref="form" id="form"> <p class="form-group"> <label for="vendor" class="col-sm-2 control-label text-left">vendor:</label> <p class="col-sm-2"> <input type="text" class="form-control vendor" id="vendor" v-model="vendor" control-label name="vendor"> </p> </p> <p class="form-group"> <label for="dType" class="col-sm-2 control-label text-left">Type:</label> <p class="col-sm-2"> <input type="text" class="form-control dType" id="dType" v-model="dType" control-label name="dType"> </p> </p> <p class="form-group"> <label for="dDesc" class="col-sm-2 control-label text-left">description:</label> <p class="col-sm-2"> <input type="text" class="form-control dDesc" id="dDesc" v-model="dDesc" control-label name="dDesc"> </p> </p> <!--<serList class="serListPad" :services="services" @services-change="servicesChange">--> <!--</serList>--> <!--发现这个serList不用抽出来组件--> <p 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>--> <p v-for="service in services"> <service v-for="item in items" :items="items" :service="service" @child-services-change="childServicesChange"></service> </p> </p> </form> <button class="btn btn-info" @click="commit">commit</button> <button class="btn btn-success">save</button> </p> </template>