Home > Web Front-end > Vue.js > body text

Detailed explanation of the principles and methods of Vue component communication

WBOY
Release: 2023-07-18 18:52:49
Original
1060 people have browsed it

Detailed explanation of the principles and methods of Vue component communication

Vue is a popular front-end development framework that facilitates developers to build interactive user interfaces. In Vue, component communication is a very important part, which can realize data transfer and interaction between components. This article will analyze in detail the principles and common methods of Vue component communication, and illustrate it through code examples.

1. Principle of component communication

Vue's component communication principle is based on the concept of "one-way data flow", that is, data flows from the parent component to the child component, and the child component cannot directly modify the parent component. The component's data. This principle makes the relationship between Vue components clearer and maintainable.

2. Props and $emit

The most commonly used component communication method in Vue is through props and $emit. Props is used to receive data passed by the parent component, and $emit is used to send data to the parent component. Send event.

  1. Parent component passes data to child component

In the parent component, data is passed through the attribute form of the child component tag:

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

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

export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      childData: 'Hello World'
    }
  }
}
</script>

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

<script>
export default {
  props: ['data']
}
</script>
Copy after login

In the above In the example, the parent component binds the data attribute of the child component through v-bind, and the child component receives it through props.

  1. The child component sends events to the parent component

In the child component, trigger a custom event through $emit and pass the data:

// 子组件
<template>
  <div>
    <button @click="sendMessage">发送消息</button>
  </div>
</template>

<script>
export default {
  methods: {
    sendMessage() {
      this.$emit('send', 'Hello World')
    }
  }
}
</script>

// 父组件
<template>
  <div>
    <child-component @send="receiveMessage"></child-component>
  </div>
</template>

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

export default {
  components: {
    ChildComponent
  },
  methods: {
    receiveMessage(message) {
      console.log(message) // 输出:Hello World
    }
  }
}
</script>
Copy after login

In the above example, the child component triggers an event called send via $emit and passes the data via parameters. The parent component listens to this event through @send and receives data in the callback function.

3. Provide and inject

In addition to props and $emit, Vue also provides a more flexible component communication method, namely provide and inject. It allows a parent component to inject a common data or method into all child components.

  1. Parent component injects data into child component

In the parent component, provide data through provide:

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

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

export default {
  components: {
    ChildComponent
  },
  provide() {
    return {
      message: 'Hello World'
    }
  }
}
</script>

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

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

In the above example, the parent component A data named message is provided through provide, and the subcomponent is injected and used through inject.

  1. The child component injects the method into the parent component

In the child component, the method of injecting the parent component is through inject:

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

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

export default {
  components: {
    ChildComponent
  },
  methods: {
    showMessage() {
      console.log('Hello World')
    }
  }
}
</script>

// 子组件
<template>
  <div>
    <button @click="showMessage">显示消息</button>
  </div>
</template>

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

In the above example , the child component injects the showMessage method of the parent component through inject, and calls it through the button click event.

4. Summary

This article analyzes in detail the principles and common methods of Vue component communication, including props and $emit, provide and inject. Through these methods, we can implement data transfer and interaction between components in Vue. Reasonable use of component communication methods can make the code clearer and maintainable, and improve development efficiency. Mastering these methods is very important for Vue developers.

The above is the detailed content of Detailed explanation of the principles and methods of Vue component 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