zoukankan      html  css  js  c++  java
  • vue v-model 与 组件化的表单组件如何沟通

    参考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>
  • 相关阅读:
    新华字典有多少字
    lisp install
    OCaml Language Sucks
    Erlang, Haskell, OCaml: screw one, marry one, kill one. Which and why?
    Linux获取网页源码的几种方法
    什么是zhcon
    What is plowshare?
    neo4j简单学习
    neo4j 云端部署
    Clojure语言 vs Scala语言
  • 原文地址:https://www.cnblogs.com/CyLee/p/9296626.html
Copyright © 2011-2022 走看看