It has been a long time since the birth of vue3, and the author has only recently started learning vue3. Compared with vue2, vue3 has undergone significant changes in writing. The most typical example is that vue3 implements data responsiveness through ref or reactive. Because of the emergence of ref and reactive, the way of passing values between parent and child components in Vue3 has also changed. Let's first look at how to write it in vue2 Parent component: <!-- Parent component --> <template> <div> <children :title="title" @getChildren="getChildren"></children> <div>Child component says: {{ childrenAsk }}</div> </div> </template> <script> import children from "./children.vue" export default { data() { return { title: "I am the value passed from the parent component", childrenAsk: "" } }, methods: { getChildren(val) { this.childrenAsk = val } } } </script> Subcomponents: <!-- Child Component --> <template> <div> <div>Value passed from the parent component: {{ title }}</div> <button @click="askToFather">Click to send to the parent component</button> </div> </template> <script> export default { props: { title: type: String } }, data() { return { askMsg: "This is what I said to the parent component" } }, methods: { askToFather() { this.$emit("getChildren", this.askMsg) } } } </script> In vue2, the value transfer from the child component to the parent component is implemented through this.$emit, but in vue3, this does not exist. In vue3, both data and functions are encapsulated in setup, so how does vue3 implement it? We know that setup in vue3 receives two parameters. The first parameter is props, which is the props value passed from the parent component to the child component. The second value is context, which represents the current context object. Now that we know this, we can implement the parent-child component value transfer of vue3. The parent-child inheritance in Vue3 is the same as the parent-child inheritance in Vue2. I will not elaborate on it again. The focus below is on the child-parent inheritance in Vue3. Parent Component <template> <div style="color: aqua">Parent component</div> <div>The child component says to me: {{ children_msg }}</div> <children :title="msg" @listen="listenToChildren"></children> {{ value }} </template> <script lang="ts"> import children from "@/views/component_emit/children.vue" import { defineComponent, ref } from "vue" export default defineComponent({ components: children, }, name: "father", setup() { let msg = "I am the parent component" let children_msg = ref("") // The function of ref is to implement responsiveness. If there is no ref, responsiveness cannot be implemented (reference data types use reactive) let listenToChildren = (val) => { children_msg.value = val // To use data wrapped by ref, you need to access its value in the form of .value} return { msg, children_msg, listenToChildren, } }, }) </script> <style></style> Subcomponents: <template> <div style="color: brown">Subcomponents</div> <!-- The method of parent-child transmission is the same as vue2--> <div>The value passed from the parent component is: {{ title }}</div> <button @click="sayToFather">Speak to the parent component</button> </template> <script lang="ts"> import { defineComponent } from "vue" export default defineComponent({ name: "children", props: { title: type: String, }, }, setup(props, context) { // The purpose of context is to obtain the context object. // If setup is written as setup(props, { emit }), the following context can be omitted const sayToFather = () => { const ask = "I am a child component, I talk to the parent component" context.emit("listen", ask) } return { sayToFather, } }, }) </script> <style></style> SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
Table of contents Introduction Instructions Actua...
The effect is as follows: The code is as follows ...
Preface When the HTML structure of a page contain...
Table of contents 1. setState() Description 1.1 U...
The installation of the rpm package is relatively...
It took me three hours to install MySQL myself. E...
I accidentally found that Vue.$set was invalid in...
background Recently, I executed a DML statement d...
By applying it, some public areas of the website c...
Moreover, an article website built with a blog pro...
Today, this post lists some great examples of circ...
Problem Record Today I was going to complete a sm...
Install PostgreSQL 11 on CentOS 7 PostgreSQL: The...
Table of contents Preface 1. Routing lazy loading...
Event Description onactivate: Fired when the objec...