Vue表單和v-model

xiaobangsky發表於2020-12-22

Vue表單

主要是按照Vue的文件,進行CRM操作,文件連結文件連結

介紹一些form表單中的基礎元件用法

  • form表單中有哪些基本元件,如:input/textarea/checkbox/radio/select/form

input

//html
<template>
  <div id="app">
    <input v-model="message" placeholder="edit me">
    <p>Message is: {{ message }}</p>
    <p>
      <button @click="message='xiaobangsky'">set message to xiaobangsky</button>
    </p>
  </div>
</template>
//js
export default {
  name: 'App',
  data(){
    return {
      message:'hi'
    }
  }
}

textarea

//html
 <div id="app">
    <textarea v-model="message" placeholder="add multiple lines"></textarea>
    <p>Message is: {{ message }}</p>
    <p>
      <button @click="message='xiaobangsky'">set message to xiaobangsky</button>
    </p>
  </div>

checkbox

//html
  <div id="app">
    <input  type="checkbox" id="jack" value="Jack" v-model="checkedNames">
    <label  for="jack">Jack</label>
    <input  type="checkbox" id="john" value="John" v-model="checkedNames">
    <label  for="john">John</label>
    <input  type="checkbox" id="mike" value="Mike" v-model="checkedNames">
    <label  for="mike">Mike</label>
    <br>
    <span>Checked names: {{ checkedNames }}</span>
  </div>
//js
export default {
  name: 'App',
  data(){
    return {
      checkedNames: []
    }
  }
}

radio

//html
<div id="app">
    <input type="radio" id="one" value="One" v-model="picked">
    <label for="one">One</label>
    <br>
    <input type="radio" id="two" value="Two" v-model="picked">
    <label for="two">Two</label>
    <br>
    <span>Picked: {{ picked }}</span>
  </div>
//js
export default {
  name: 'App',
  data(){
    return {
      picked: ''
    }
  }
}

select

//html
 <div id="app">
    <select v-model="selected">
      <option disabled value="">請選擇</option>
      <option>A</option>
      <option>B</option>
      <option>C</option>
    </select>
    <span>Selected: {{ selected }}</span>
  </div>
//js
export default {
  name: 'App',
  data(){
    return {
      selected: ''
    }
  }
}

form

//html
  <div id="app">
    <form @submit.prevent="submit">
      <label>
        <span>使用者名稱</span>
        <input type="text" v-model="user.username"/>
      </label>
       <label>
        <span>密碼</span>
        <input type="password" v-model="user.password"/>
      </label>
      <button type="submit">登入</button>
    </form>
  </div>
//js
export default {
  name: 'App',
  data(){
    return {
      user: {
        username:'',
        password:''
      }
    }
  },
  methods:{
    submit(){
      console.log(this.user);
    }
  }
}

修飾符

  • 修飾符:.lazy/.number/.trim

.lazy

 <span>使用者名稱</span>
 <input type="text" v-model.lazy="user.username"/>

.number

<input type="password" v-model.number="user.password"/>

.trim

<input type="text" v-model.trim="user.username"/>

v-model

程式碼示例

//v-model實際上等價於v-bind:value和v-on:input
<input type="text" :value="user.username" @input="user.username=$event.target.value"/>

自定義input

<template>
    <div class="red">
        <input :value="value" @input="onInput"/>
    </div>
</template>
<script>
export default {
    name:'MyInput',
    props:{
        value:{
            type:String
        }
    },
    methods:{
        onInput(e){
            const value =e.target.value;
            this.$emit('input',value)
        }
    }
}
</script>
<style scoped>
.red{
    background: red;
    display: inline-block;
}
</style>

//主檢視
 <my-input :value="user.username" @input="user.username=$event"/>
 import MyInput from './MyInput.vue';
export default {
  components: { MyInput },
  name: 'App',
  data(){
    return {
      user: {
        username:'',
        password:0
      }
    }
  }
}

總結

  • v-model可以繫結一個變數
  • v-model實際上等價於v-bind:value和v-on:input
  • 當為原生元件時,v-on:input=“xxx= e v e n t . t a r g e t . v a l u e " , 當 為 自 定 義 組 件 時 , v − o n : i n p u t = " x x x = event.target.value",當為自定義元件時,v-on:input="xxx= event.target.value"von:input="xxx=event”

ant-design-vue的使用

<template>
  <div id="app">
    <a-input placeholder="Basic usage" />
  </div>
</template>

<script>

export default {
  name: 'App',
  data(){
    return {
      user: {
        username:'',
        password:0
      }
    }
  },
  methods:{
    submit(){
      console.log(this.user);
    }
  }
}
</script>

import Vue from 'vue'
import App from './App.vue'
import AntD from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css'; // or 'ant-design-vue/dist/antd.less'

Vue.config.productionTip = false
Vue.use(AntD);

new Vue({
  render: h => h(App),
}).$mount('#app')

相關文章