Vue component communication: use v-bind directive for property binding communication

WBOY
Release: 2023-07-08 06:32:01
Original
2006 people have browsed it

Vue component communication: Use the v-bind directive for property binding communication

In Vue, components are an important part of building web applications. And communication between components is a very important topic. Vue provides a variety of ways to implement communication between components, one of the important ways is to use the v-bind directive for property binding communication.

The v-bind directive allows us to pass data to child components in the parent component and receive this data through the props attribute in the child component. In this way, data can be transferred and shared between parent components and child components.

Suppose we have a parent component App and a child component Child, and we want to define a property in the parent component and pass it to the child component.

First, define a data attribute message in the parent component App:

<template>
  <div>
    <h1>父组件</h1>
    <input type="text" v-model="message">
    <child-component :text="message"></child-component>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  name: 'App',
  components: {
    ChildComponent
  },
  data() {
    return {
      message: ''
    };
  }
};
</script>
Copy after login

In the child component Child, we can receive the passed attributes through the props attribute and display them in the template:

<template>
  <div>
    <h2>子组件</h2>
    <p>{{ text }}</p>
  </div>
</template>

<script>
export default {
  name: 'ChildComponent',
  props: {
    text: {
      type: String,
      required: true
    }
  }
};
</script>
Copy after login

In the above code, we pass the message attribute in the parent component to the text attribute of the child component through the v-bind directive. The subcomponent defines the received attribute types and required requirements through the props attribute. Then, in the template of the subcomponent, we can use {{ text }} to display the received attribute value.

When we enter information in the parent component, the child component will update the display in real time. This is because the data in the parent component changes, and the v-bind directive triggers a re-rendering of the child component and passes the latest property values ​​to it.

In addition to one-way attribute binding, we can also use the v-bind directive for two-way binding. This allows child components to modify property values ​​in the parent component. In the child component, we can use the v-model directive to bind a local data and trigger a custom event through the $emit method to notify the parent component of changes in property values.

The following is an example with two-way binding:

<template>
  <div>
    <h1>父组件</h1>
    <input type="text" v-model="message">
    <child-component v-bind:text="message" @input="message = $event"></child-component>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  name: 'App',
  components: {
    ChildComponent
  },
  data() {
    return {
      message: ''
    };
  }
};
</script>
Copy after login
<template>
  <div>
    <h2>子组件</h2>
    <input type="text" v-model="text" @input="$emit('input', text)">
  </div>
</template>

<script>
export default {
  name: 'ChildComponent',
  props: {
    text: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      text: this.text
    };
  }
};
</script>
Copy after login

In the above code, the message attribute in the parent component is passed to the child component through the v-bind directive. In the subcomponent, the received attribute is bound to a local text attribute through the v-model directive, and two-way binding is performed in the input box. When the value of the input box changes, the child component triggers the input event through the $emit method and passes the current text attribute value to the parent component as a parameter. The parent component listens to the custom event through @input and updates the value of the message attribute in the event handler.

Property binding communication through the v-bind directive allows us to easily share data between Vue components. It is a very important and commonly used component communication method in Vue. In actual development, we can choose appropriate communication methods according to specific needs and scenarios to achieve interaction and data sharing between components.

The above is the detailed content of Vue component communication: use v-bind directive for property binding communication. 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