1. 程式人生 > >vue v-model 與 組件化的表單組件如何溝通

vue v-model 與 組件化的表單組件如何溝通

field div pla tle ren input 代碼 mod end

參考mint-ui的代碼:

https://github.com/ElemeFE/mint-ui/blob/master/packages/radio/src/radio.vue

https://github.com/ElemeFE/mint-ui/blob/master/packages/field/src/field.vue

直接上代碼

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
    <style>
    </style>

    <body>
        <div id="app">
            <div v-for
=‘(item, index) in items‘ :key=‘index‘> <myradio v-model="picked" :text="item"></myradio> </div> <br> <span>Picked: {{ picked }}</span> </div> </body> <script> // 局部註冊組件 var
myradio = Vue.extend({ data: function () { return { currentValue: this.value } }, props: { value: ‘‘, text: ‘‘ }, template: `
<label> <input type="radio" id="two" :value="text" v-model="currentValue"> <label for="two">{{ text }}</label> </label> `, watch: { value(val) { this.currentValue = val; }, currentValue(val) { this.$emit(‘input‘, val); } } }); Vue.component(‘myradio‘, myradio) new Vue({ el: ‘#app‘, data: { picked: ‘Three‘, items: [‘One‘, ‘Two‘, ‘Three‘] } }) </script> </html>

vue v-model 與 組件化的表單組件如何溝通