自定义输入框v-modal,2.0写法

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
</head>

<body>
    <div id="app">
        <my-input v-model="msg" />
    </div>
    <script type="text/javascript">
        Vue.component('myInput', {
            template: '<input :value="value" ref="ipt" @input="change_input_handle" />',
            props: {
                value: String
            },
            methods: {
                change_input_handle() {
                    this.$emit('input', this.$refs.ipt.value);
                }
            }
        })
        new Vue({
            el: "#app",
            data() {
                return {
                    msg: '测试'
                }
            },
            watch:{
                msg(value){
                    console.log(value)
                }
            }
        })
    </script>
</body>

</html>

自定义v-modal,3.0写法

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
</head>

<body>
    <div id="app">
        <my-input v-model="msg" />
    </div>
    <script type="text/javascript">
        Vue.component('myInput', {
            template: '<input :value="modelValue" ref="ipt" @input="change_input_handle" />',
            props: ['modelValue'],
            methods: {
                change_input_handle() {
                    this.$emit('update:modelValue', this.$refs.ipt.value);
                }
            }
        })
        new Vue({
            el: "#app",
            data() {
                return {
                    msg: '测试'
                }
            },
            watch:{
                msg(value){
                    console.log(value)
                }
            }
        })
    </script>
</body>

</html>

  

posted on 2021-01-25 16:44  随心的博客  阅读(750)  评论(0编辑  收藏  举报