Home > Web Front-end > Vue.js > Methods and scenario analysis of value transfer between parent-child components in Vue

Methods and scenario analysis of value transfer between parent-child components in Vue

PHPz
Release: 2023-06-09 16:12:13
Original
1345 people have browsed it

With the continuous development of Vue technology, more and more front-end developers are beginning to use the Vue framework for development. In the Vue framework, component development is a very important concept. Data passing between components is a very common problem, especially between parent and child components. In this article, we will discuss the method and applicable scenarios of value transfer between parent and child components in Vue.

Parent-child components in Vue

In the Vue framework, parent-child components are a common component relationship. Generally, the parent component is responsible for managing child components, and the child components are responsible for rendering data and event processing. This component relationship is very flexible and can easily achieve modular development and reuse of large-scale applications.

Data transfer methods between parent and child components

There are many ways to achieve data transfer between parent and child components in Vue. Below we will introduce these methods one by one.

  1. props

props is the most commonly used method of passing values ​​between parent and child components in Vue. Through props, parent components can pass data to child components. Child components can declare the required properties through the props option, and then the parent component can pass the data to the child component. For example:

// 父组件
<template>
  <div>
    <child-component :title="title"></child-component>
  </div>
</template>

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

export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      title: '这是一个标题'
    }
  }
}
</script>

// 子组件
<template>
  <div>
    {{ title }}
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: ''
    }
  }
}
</script>
Copy after login

The parent component passes the title to the child component through :title, and the child component receives the title data through the props option. This method is suitable for simple data transfer, especially in the case of one-way data flow.

  1. emit

emit is an event mechanism that allows child components to pass data to parent components. The child component can trigger an event through the $emit method, and then the parent component can listen to the event and handle it through the v-on directive. For example: The $emit method in the

// 父组件
<template>
  <div>
    <child-component @onTitleChange="handleTitleChange"></child-component>
  </div>
</template>

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

export default {
  components: {
    ChildComponent
  },
  methods: {
    handleTitleChange(newTitle) {
      console.log(newTitle)
    }
  }
}
</script>

// 子组件
<template>
  <div>
    <button @click="changeTitle">Change Title</button>
  </div>
</template>

<script>
export default {
  methods: {
    changeTitle() {
      this.$emit('onTitleChange', '新的标题')
    }
  }
}
</script>
Copy after login

subcomponent triggers an event named onTitleChange and passes the new title. The parent component listens to this event through @onTitleChange and passes a callback function for processing. This method is suitable for situations where bidirectional data flow between child components and parent components is required.

  1. provide/inject

provide/inject is also a way for parent-child components to pass values, but it is not limited to parent-child components. It enables descendant components to share data. Data can be provided to child components through the parent component's provide option. Child components can receive data through the inject option. For example:

// 父组件
<template>
  <div>
    <child-component></child-component>
  </div>
</template>

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

export default {
  components: {
    ChildComponent
  },
  provide() {
    return {
      title: '这是一个标题'
    }
  }
}
</script>

// 子组件
<template>
  <div>
    {{ title }}
  </div>
</template>

<script>
export default {
  inject: ['title']
}
</script>
Copy after login

The parent component provides the title to the child component through the provide option, and the child component receives the title data through the inject option. This method is suitable for data transfer or sharing between cross-level components.

  1. $parent and $children
$parent和$children这两个属性也可以实现父子组件之间的数据传递。$parent可以访问父组件的实例,$children可以访问子组件的实例。通过这两个属性可以直接访问父子组件实例的数据和方法。但这种方法并不推荐使用,因为它们是耦合度非常高的方法,不利于组件的复用和维护。
Copy after login

Applicable scenario analysis

The above are several methods for passing values ​​​​from parent-child components in Vue. Different methods are applicable for different scenarios. Let us analyze the applicable scenario issues below.

  1. #props

When data flows in one direction and the child component needs to render data based on the data passed by the parent component, or the data needs to be processed before rendering, you can use props .

  1. emit

Emit should be used when you need to process data in a child component and pass the processed data to the parent component. For example: a form component in a child component needs to pass the form data to the parent component for submission after filling out the form.

  1. provide/inject

Provide/inject should be used when you need to share data among cross-level components and do not want to use a view hierarchy.

  1. $parent and $children

This method is not recommended, but if you only need to directly access the instances of the parent and child components in a specific situation, you can Consider using. For example: In a specific scenario, you need to directly operate an instance method of a subcomponent, and this instance method is only defined in the subcomponent.

In short, in Vue, by rationally using the above four parent-child component value transfer methods, we can easily achieve communication and data interaction between components.

The above is the detailed content of Methods and scenario analysis of value transfer between parent-child components in Vue. 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