vue2.0數(shù)據(jù)雙向綁定與表單bootstrap+vue組件

最近一直在用vue,覺得確實是好用。

成都創(chuàng)新互聯(lián)是專業(yè)的聶拉木網(wǎng)站建設(shè)公司,聶拉木接單;提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行聶拉木網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

一,拿數(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>

vue2.0數(shù)據(jù)雙向綁定與表單bootstrap+vue組件

vue中的所有數(shù)據(jù)都是在data中定義的,

el是指的掛載的元素,

watch 是我可以檢測某個數(shù)據(jù)的變化。

v-model=“name” 就是與data中的name數(shù)據(jù)綁定,input框中的值變,那么data中的name也會變,我們可以通過差值操作,也就是{{name}}來看到變化,當然也可以像我一樣打log。都是可以的。

當然這樣也許還不是很實用,官網(wǎng)上也是這么介紹的,那么就說我在工作中是怎么用的吧

vue2.0數(shù)據(jù)雙向綁定與表單bootstrap+vue組件

現(xiàn)在我的需求是要得到我表單里邊的所有value ,我們也許可以       

 let service = $('.vendor').val();
        let vendor = document.getElementsByClassName('vendor')[0].value;

但是這樣就完全沒有g(shù)et到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組件

以上所述是小編給大家介紹的vue2.0數(shù)據(jù)雙向綁定與表單bootstrap+vue組件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

標題名稱:vue2.0數(shù)據(jù)雙向綁定與表單bootstrap+vue組件
本文鏈接:http://bm7419.com/article42/jjsgec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、企業(yè)建站、動態(tài)網(wǎng)站、網(wǎng)站排名手機網(wǎng)站建設(shè)、響應式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計