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 introduce Link start Continue t...
Original text: http://www.planabc.net/2008/08/05/...
In my last post I talked about how to make a web p...
Effect If you use it, please optimize the code an...
Database transaction isolation level There are 4 ...
We know that MySQL is a persistent storage, store...
This article shares the specific code of JavaScri...
MySQL is easy to install, fast and has rich funct...
Application scenarios: One of the new requirement...
Original derivative command: bin/sqoop import -co...
Discovering Needs If only part of an area is allo...
This article example shares the specific code of ...
This article uses the element official website an...
Table of contents linux 1. What is SWAP 2. What d...
1 Introduction Binary log records SQL statements ...