在 vue 中使用 v-model 监听子组件中的任何值

背景:
最近在做博客的时候,我想把editor做成一个单独的组件,但是又想用 element-ui 的表单验证来限制editor的内容,于是想在组件上使用 v-model 就像使用 input 一样,于是去 vue 官方文档搜了下 v-model, 有这么个东西


v-model.jpg

原理:
v-model 是个语法糖,实际就是在触发 input 事件时去更新 value 值,从文档看,可以在子组件用 model 来指定 v-model 要监听的事件,那么实现思路就是:在子组件 watch v-model 要监听的值,用 model 改变监听的 event 为 watch,在 watch 到值变化时 $emit 返回给父组件值,应该就可以实现 v-model 了

代码:
vue-cli 初始化一个空项目 app 为父组件,HelloWorld 为子组件
app 绑定数据并展示一下:

<template>
    <div id="app">
        <HelloWorld v-model="parentMsg" />

        <div style="margin-top:20px;">
            parentMsg is: {{ parentMsg }}
        </div>
    </div>
</template>

<script>
    import HelloWorld from './components/HelloWorld.vue'
    export default {
        name: 'app',
        components: {
            HelloWorld
        },
        data(){
            return{
                parentMsg: 100
            }
        }
    }
</script>

HelloWorld:

<template>
    <div class="hello">
        <h1>childMsg is: {{ childMsg }}</h1>

        <button @click="childMsg += 1">
            click to change childMsg
        </button>
    </div>
</template>

<script>
    export default {
        name: 'HelloWorld',
        model: {
            prop: 'parentMsg',
            // v-model 监听 watch 事件
            event: 'watch'
        },
        props: {
            parentMsg: Number
        },
        data(){
            return{
                childMsg: 0
            }
        },
        watch: {
            // 监听数据变化 传送给父组件
            childMsg(newVal){
                this.$emit('watch', newVal)
            }
        },
        mounted() {
            // 初始化 childMsg
            this.childMsg = this.parentMsg
        }
    }
</script>

这样就实现了数据的实时获取:


v-model-res.jpg

写在最后:
感觉其实 相当于 给子组件传了一个事件, 在子组件 watch 要绑定的值,值变化时把值传回父组件,在父组件更新视图,就是子组件给父组件传值的操作。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 主要还是自己看的,所有内容来自官方文档。 介绍 Vue.js 是什么 Vue (读音 /vjuː/,类似于 vie...
    Leonzai阅读 3,399评论 0 25
  • 1.vue.js的两个核心是什么? vue.js的两个核心分别是数据驱动(MVVM)和组件化。 一、数据驱动 数据...
    fengcol阅读 1,075评论 0 3
  • 以下内容是我在学习和研究Vue时,对Vue的特性、重点和注意事项的提取、精练和总结,可以做为Vue特性的字典; 1...
    科研者阅读 14,171评论 3 24
  • VUE介绍 Vue的特点构建用户界面,只关注View层简单易学,简洁、轻量、快速渐进式框架 框架VS库库,是一封装...
    多多酱_DuoDuo_阅读 2,763评论 1 17
  • vue.js是什么 是一套构建用户界面的渐进式框架 vue应用组成 一个 Vue 应用由一个通过new Vue创建...
    多多酱_DuoDuo_阅读 1,046评论 0 2