Home Web Front-end JS Tutorial Analysis of v-model directive in Vue (with code)

Analysis of v-model directive in Vue (with code)

Jul 27, 2018 pm 01:07 PM
v-model directive

The content of this article is about the analysis of the v-model instruction in Vue (with code). It has a good reference value and I hope it can help friends in need.

1. Instruction explanation

v-model creates a two-way binding on a form control or component, which is essentially responsible for monitoring user input events (onchange, onkeyup, onkeydown, etc., which one specifically , please also check the official underlying implementation documentation) to update the data and perform some special processing for some extreme scenarios.

2. Form input binding methods and methods

2.1 Input box implements two-way data binding example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

      <label>请输入:</label><input type="text" v-model="inputs"/><br/>

      <label>你在input框中输入了:</label><span>{{inputs}}</span>

    </div>

  </div>

</template>

 

<script>

    export default {

        name:"v-model",

      data(){

          return {

            title: &#39;v-model学习&#39;,

            inputs: &#39;&#39;

          }

      }

    }

</script>

 

<style scoped>

  .p1{

  text-align: left;

  }

  .spancss1{

      float: left;

      }

</style>

Copy after login

2.2 Textarea multiple Example of double-thinking data binding using line text:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

      <label>请输入:</label><textarea v-model="text"></textarea><br/>

      <label>你在textarea框中输入了:</label><span>{{text}}</span>

    </div>

  </div>

</template>

 

<script>

  export default {

    name:"v-model",

    data(){

      return {

        title: &#39;v-model学习&#39;,

        text: &#39;&#39;

      }

    }

  }

</script>

 

<style scoped>

  .p1{

    text-align: left;

  }

  .spancss1{

    float: left;

  }

</style>

Copy after login

2.3 Check box data binding

Single check box data binding Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

      <label>请输入:</label><input type="checkbox" id="checkbox" v-model="checked">选我<br/>

      <label v-if="checked">选中了:</label>

      <label v-if="!checked">没选中:</label>

      <span>{{ checked }}</span>

    </div>

  </div>

</template>

 

<script>

  export default {

    name:"v-model",

    data(){

      return {

        title: &#39;v-model学习&#39;,

        checked: &#39;&#39;

      }

    }

  }

</script>

 

<style scoped>

  .p1{

    text-align: left;

  }

  .spancss1{

    float: left;

  }

</style>

Copy after login

Multiple check boxes to implement data binding Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

      <label>请输入:</label>

      <input type="checkbox" id="vue1" value="vue1" v-model="checkedValues">vue1

      <input type="checkbox" id="vue2" value="vue2" v-model="checkedValues">vue2

      <input type="checkbox" id="vue3" value="vue3" v-model="checkedValues">vue3

      <br/>

      <label>选中了:</label><span>{{ checkedValues }}</span>

    </div>

  </div>

</template>

 

<script>

  export default {

    name:"v-model",

    data(){

      return {

        title: &#39;v-model学习&#39;,

        checkedValues: []

      }

    }

  }

</script>

 

<style scoped>

  .p1{

    text-align: left;

  }

  .spancss1{

    float: left;

  }

</style>

Copy after login

2.4 Radio buttons implement data binding, and the selected radio button value is obtained and displayed:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

      <input type="radio" id="radio1" value="1" v-model="checked">男<br/>

      <input type="radio" id="radio2" value="2" v-model="checked">女<br/>

      <label> 选中了:{{ checked }}</label>

    </div>

  </div>

</template>

 

<script>

  export default {

    name:"v-model",

    data(){

      return {

        title: &#39;v-model学习&#39;,

        checked: &#39;&#39;

      }

    }

  }

</script>

 

<style scoped>

  .p1{

    text-align: left;

  }

  .spancss1{

    float: left;

  }

</style>

Copy after login

2.5 Introduction to the selection box

Method 1: The selection box implements two-way data binding. First, the selection box implements a single selection. When the options in the radio button box set the disabled attribute, the options cannot be selected. When When the options in the radio button box have no value, the text of the options will be bound when the options are selected. When the options in the radio button box have a value, the value of the options will be bound when the options are selected. The example is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

      <select v-model="selected">

        <option disabled value="">请选择</option>

        <option>A</option>

        <option value="2">B</option>

        <option value="3">C</option>

      </select>

      <span>Selected: {{ selected }}</span>

    </div>

  </div>

</template>

 

<script>

  export default {

    name:"v-model",

    data(){

      return {

        title: &#39;v-model学习&#39;,

        selected: &#39;&#39;

      }

    }

  }

</script>

 

<style scoped>

  .p1{

    text-align: left;

  }

  .spancss1{

    float: left;

  }

</style>

Copy after login

Method 2 introduces the situation of multiple selections in the selection box. First, you need to add the multiple attribute to the select. When the options are in the radio button When setting the disabled attribute, this option cannot be selected. When the options in the radio button box have no value, the text of this option will be bound when this option is selected. When the options in the radio button box have a value, when this option is selected, The value of this options will be bound, then press the ctrl key and click on the options to be selected. A simple example is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

      <select v-model="selected" multiple>

        <option disabled value="">请选择</option>

        <option>A</option>

        <option value="2">B</option>

        <option value="3">C</option>

      </select>

      <span>Selected: {{ selected }}</span>

    </div>

  </div>

</template>

 

<script>

  export default {

    name:"v-model",

    data(){

      return {

        title: &#39;v-model学习&#39;,

        selected: []

      }

    }

  }

</script>

 

<style scoped>

  .p1{

    text-align: left;

  }

  .spancss1{

    float: left;

  }

</style>

Copy after login

3. v-model modifier

3.1 .lazy

By default, v-model synchronizes the value of the input box with the data after each input event is triggered. (Except when input method combines text). You can add the lazy modifier to switch to using onchange event is synchronized. When data is entered in the input box, the data will not change immediately. When the cursor leaves the input box, the data will be changed synchronously. The sample code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

      <!-- 在“change”时而非“input”时更新 -->

      <input type="text" id="names" v-model.lazy="msg" ><br/>

      <label>输入值:</label>{{msg}}

    </div>

  </div>

</template>

 

<script>

  export default {

    name:"v-model",

    data(){

      return {

        title: &#39;v-model学习&#39;,

        msg: &#39;&#39;

      }

    }

  }

</script>

 

<style scoped>

  .p1{

    text-align: left;

  }

  .spancss1{

    float: left;

  }

</style>

Copy after login

3.2 .number

If you want to automatically convert the user’s input value to a numerical type, define type as number type and add the number modifier to v-model , when the user enters numerical type data, v-model.number will automatically convert the input data into a numerical type. Note that if the user enters the special letter e, the number attribute cannot be recognized. A simple code example is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

      <!-- 在“change”时而非“input”时更新 -->

      <input type="number" id="names" v-model.number="msg" ><br/>

      <label>输入值:</label>{{msg}}

    </div>

  </div>

</template>

 

<script>

  export default {

    name:"v-model",

    data(){

      return {

        title: &#39;v-model学习&#39;,

        msg: &#39;&#39;

      }

    }

  }

</script>

 

<style scoped>

  .p1{

    text-align: left;

  }

  .spancss1{

    float: left;

  }

</style>

Copy after login

3.3 .trim

If you want to automatically filter the leading and trailing blank characters entered by the user, you can add the trim modifier to v-model, in Add a few more spaces at the beginning of the input box. When the cursor leaves, the trim attribute will automatically filter the ending spaces. A simple code example is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

      <label>无trim属性</label>

      <input type="text" id="names" v-model="msg1" ><br/>

      <label>输入值:</label>{{msg1}}<br/>

      <label>有trim属性</label>

      <input type="text" id="names2" v-model.trim="msg2" ><br/>

      <label>输入值:</label>{{msg2}}

    </div>

  </div>

</template>

 

<script>

  export default {

    name:"v-model",

    data(){

      return {

        title: &#39;v-model学习&#39;,

        msg1: &#39;&#39;,

        msg2: &#39;&#39;

      }

    }

  }

</script>

 

<style scoped>

  .p1{

    text-align: left;

  }

  .spancss1{

    float: left;

  }

</style>

Copy after login

4. Used in components v-model

4.1 自定义组件v-model

一个组件上的 v-model 默认会利用名为 value 的 prop 和名为 input 的事件,但是像输入框、单选框、复选框等类型的输入控件可能会将 value 特性用于不同的目的。本文以自定义输入框和onchange事件为例,在输入框输入值,当光标离开以后输入值会输出绑定到置顶位置:

1. 在index中声明组件(在index声明的组件为全局组件,全局可用):

1

2

3

4

5

6

7

8

9

10

11

Vue.component(&#39;base-text&#39;, {

  model: {

    prop: &#39;value&#39;,

    event: &#39;change&#39;

  },

  props: {

    checked: Boolean

  },

  template: `<input type="text" id="inputs" v-on:change="$emit(&#39;change&#39;, $event.target.value)">`

});

=

Copy after login

2. 在要使用该组件的插件中声明:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<template>

  <div>

    <p class="p1">{{title}}</p>

    <div class="spancss1">

     <base-text v-model="msg"></base-text><br/>

      <label>你输入了:</label>{{msg}}

    </div>

  </div>

</template>

 

<script>

  export default {

    name:"v-model",

    data(){

      return {

        title: &#39;v-model学习&#39;,

        msg: &#39;&#39;

      }

    }

  }

</script>

 

<style scoped>

  .p1{

    text-align: left;

  }

  .spancss1{

    float: left;

  }

</style>

Copy after login

3. 结果示例如下:

相关推荐:

对4-v-bind指令的分析(附代码)

The above is the detailed content of Analysis of v-model directive in Vue (with code). 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Zustand asynchronous operation: How to ensure the latest state obtained by useStore? Zustand asynchronous operation: How to ensure the latest state obtained by useStore? Apr 04, 2025 pm 02:09 PM

Data update problems in zustand asynchronous operations. When using the zustand state management library, you often encounter the problem of data updates that cause asynchronous operations to be untimely. �...

See all articles