vue自定义v-modal


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

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM