自定义输入框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 >
        <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 >
        <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>

  

相关文章:

  • 2022-01-11
  • 2021-11-22
  • 2022-02-19
  • 2021-09-21
  • 2022-01-18
  • 2022-12-23
猜你喜欢
  • 2021-11-09
  • 2020-07-16
  • 2021-11-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案