vue sets v-model to the component

WBOY
Release: 2023-05-24 10:01:37
Original
1056 people have browsed it

Vue is a popular front-end framework that uses a componentized approach to build complex applications. In Vue, v-model is a special syntax used to two-way bind the value of a component to the data of the parent component. By default, v-model can only be used with common HTML form elements such as text boxes, radio buttons, and check boxes. However, we can extend the scope of v-model through custom directives or plug-ins so that it can be applied to custom components.

In this article, we will introduce how to use v-model to set up two-way binding for custom components, and analyze the implementation principle of v-model.

Set v-model for custom components

Suppose we have created a custom component named MyInput, and its usage in HTML is as follows:

<my-input :value="message" @input="message = $event"></my-input>
Copy after login

This The component receives a value attribute as its value and sends value changes to the parent component through the input event. Now we want to use v-model syntax to simplify the usage of this component. We only need to bind v-model to the message variable:

<my-input v-model="message"></my-input>
Copy after login

In this way, the message variable in the parent component and the MyInput component The value attribute will be two-way bound.

So, how do we implement this function? First, we need to define the value attribute in the props option of the MyInput component:

props: {
  value: {
    type: String,
    default: ''
  }
}
Copy after login

The props feature in Vue is used here. We define an attribute named value, the type is String, and set the default value to Empty string.

Next, we need to add an event listener named input to the MyInput component to respond to value changes in the parent component. When the parent component updates the value attribute, this listener will be triggered:

methods: {
  handleInput(event) {
    this.$emit('input', event.target.value);
  }
}
Copy after login

This method receives an event object, sends an input event to the parent component through the $emit method, and passes the latest value at the same time.

Finally, in the template of the MyInput component, we need to bind the value attribute to the real form element and listen to the input event:

<template>
  <input :value="value" @input="handleInput">
</template>
Copy after login

This template uses Vue’s template syntax , use: value to bind the value attribute, call the handleInput method in the input event, and trigger the input event.

In this way, we have successfully set up the v-model two-way binding function for the custom component.

Implementation principle of v-model

In the above example, we use v-model to simplify the use of custom components. However, v-model is actually a syntactic sugar, and its complete usage is: v-model="message" => :value="message" @input="message = $event".

As you can see, v-model actually binds an attribute named value and updates the corresponding variable in the input event. This process is actually completed by the v-bind and v-on instructions in Vue. Through the v-bind instruction, we bind the value attribute to the variable message; through the v-on instruction, we listen to the input event and pass the $event object to the message variable.

So, how can we apply v-model to custom components? The answer is also very simple. We only need to add an attribute named model to the component and set it to true:

props: {
  value: {
    type: String,
    default: ''
  }
},
model: {
  prop: 'value',
  event: 'input'
}
Copy after login

This model attribute tells Vue that we want to use the value attribute as the attribute of the v-model of the component. name, and emits an event named input in the input event. Vue will automatically bind this property to the custom component, thus realizing two-way binding of v-model.

Of course, for more complex custom components, we may need additional logic to handle v-model binding. However, Vue's core code already provides good encapsulation and extensibility, allowing us to easily implement this function. For developers who are new to Vue, understanding the implementation principle of v-model will also help improve their understanding of the Vue framework.

Conclusion

In this article, we introduced how to use v-model to set up two-way binding for custom components, and analyzed the implementation principle of v-model. v-model is an important feature in the Vue framework. It greatly simplifies the interaction of data between components and saves developers a lot of code. I hope this article can be helpful to Vue beginners, and also provide some inspiration to developers who have already mastered Vue.

The above is the detailed content of vue sets v-model to the component. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template