Table of Contents
vue2.0 implementation method
The most common thing is to control the display and closing of the modal box. We can also use v-model to take the el-dialog component of element as an example
Home Web Front-end Vue.js An article explaining in detail how vue implements v-model (with code examples)

An article explaining in detail how vue implements v-model (with code examples)

Jan 06, 2023 pm 04:40 PM
front end vue.js

This article brings you relevant knowledge about vue. It mainly introduces to you why we use v-model? How to use vue to implement v-model? If you are interested, let’s take a look. I hope it will be helpful to everyone.

An article explaining in detail how vue implements v-model (with code examples)

  • Why use v-model? As a two-way binding instruction, v-model is also one of the two core functions of vue. It is very convenient to use and improves the efficiency of front-end development. In the view layer, the model layer needs data interaction with each other, so v-model can be used.
  • A brief description of the principle of v-model

v-model mainly provides two functions. The input value of the view layer affects the attribute value of the data. If the value of the data attribute changes, the view will be updated. The value of the layer changes.

The core is that on the one hand, the modal layer hijacks each attribute through defineProperty, and once changes are detected, it is updated through the relevant page elements. On the other hand, by compiling the template file, the input event is bound to the v-model of the control, so that the page input can update the relevant data attribute values ​​in real time.

  • What is v-model v-model is Vue's two-way binding instruction. It can synchronously update the value input by the control on the page to the relevant bound data attribute. It will also update the value of the input control on the page when the data binding attribute is updated.

vue2.0 implementation method

  • Parent component
<template>
  <div id="app">
    {{username}} <br/>
    <my-input type="text" v-model="username"></my-input>
  </div>
</template>

<script>
import myinput from './components/myinput'
export default {
  name: 'App',
  components:{
      myinput
  },
  data(){
    return {
      username:''
    }
  }

}
</script>
Copy after login
  • myinput.vue:
<template>
    <div class="my-input">
        <input type="text" class="my-input__inner" @input="handleInput"/>
    </div>
</template>

<script>
    export default {
        name: "myinput",
        props:{
            value:{ //获取父组件的数据value
                type:String,
                default:''
            }
        },
        methods:{
            handleInput(e){
                this.$emit('input',e.target.value) //触发父组件的input事件
            }
        }
    }
</script>
Copy after login

The most common thing is to control the display and closing of the modal box. We can also use v-model to take the el-dialog component of element as an example

  • App.vue
<template>
    <div>
        <kmDialog
            v-model="showDialog"
        >
        <el-button @click="show">点我</el-button>
    </div>
</template>
<script>
    import kmDialog from 'KmDialog.vue'
    export default {
        components: {
            KmDialog
        }
        data () {
            return {
                showDialog: false
            }
        },
        methods: {
            show() {
                this.showDialog = true
            }
        }
    }
</script>
Copy after login
  • KmDialog.vue
<template>
    <el-dialog
        :title="isEdit ? '编辑设备' : '新增设备'"
        :visible.sync="value"
        width="40%"
        @close="cancel"
      >
        <span>这是一段信息</span>
    </el-dialog>
</template>
<script>
    export default {
        props: {
            value: {
                default: false,
                type: Boolean
            }
        },
        methods: {
            cancel() {
                this.$emit('input', false)
            }
        }
    }
</script>
Copy after login

Recommended learning: "vue.js Video Tutorial"

The above is the detailed content of An article explaining in detail how vue implements v-model (with code examples). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

An article about memory control in Node

Detailed graphic explanation of how to integrate the Ace code editor in a Vue project Detailed graphic explanation of how to integrate the Ace code editor in a Vue project Apr 24, 2023 am 10:52 AM

Detailed graphic explanation of how to integrate the Ace code editor in a Vue project

Explore how to write unit tests in Vue3 Explore how to write unit tests in Vue3 Apr 25, 2023 pm 07:41 PM

Explore how to write unit tests in Vue3

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

Let's talk in depth about the File module in Node

PHP and Vue: a perfect pairing of front-end development tools PHP and Vue: a perfect pairing of front-end development tools Mar 16, 2024 pm 12:09 PM

PHP and Vue: a perfect pairing of front-end development tools

How to solve cross-domain issues? A brief analysis of common solutions How to solve cross-domain issues? A brief analysis of common solutions Apr 25, 2023 pm 07:57 PM

How to solve cross-domain issues? A brief analysis of common solutions

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

Questions frequently asked by front-end interviewers

Learn more about Buffers in Node Learn more about Buffers in Node Apr 25, 2023 pm 07:49 PM

Learn more about Buffers in Node

See all articles