


How to communicate between parent and child components in Vue? Let's talk about the methods of passing on from father to son and from son to father.
How to communicate between parent and child components in Vue? The following article will take you to understand the communication between Vue parent and child components, and introduce the methods of parent passing value to child and child passing value to parent. I hope it will be helpful to everyone.
Vue parent-child component
What is a parent-child component?
Introduce another component into a component, and the introduced component is called a sub-component. Because of the modularization of Vue, the common parts will be extracted into a separate module, and all page content will not be included. Written in a vue file, due to modularity, communication problems between the two modules cannot be avoided. At this time, there is the problem of data transfer between modules (components). [Related recommendations: "vue.js Tutorial"]
Vue parent-child component communication
In vue, one component often uses the data of another component or Method, at this time there is a communication problem between parent and child components
Vue parent-to-child
1. Look at the code first, and explain it below
<body> //父组件 <div id="app"> <cpn3 :ctitle="title"></cpn3> </div> //子组件 <template id="cpn3"> <div> <h1 id="ctitle">{{ctitle}}</h1> <p>orange</p> </div> </template> <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> <script> //子组件 Vue.component("cpn3", { template: "#cpn3", // 组件里的data是一个函数,必须要返回一个对象,变量写在返回对象里 props: { ctitle: { type: String, default () { return {} } } } }) //父组件 const app = new Vue({ el: "#app", data: { title: 'orange' }, methods: {}, }) </script> </body>
Explanation:
1. First write the basic template separation component
2. Add props attributes to the sub-component in object mode. In the props, you can write the parameters you need to pass. The parameters are also in object mode. Code It’s clearer,
//子组件 Vue.component("cpn3", { template: "#cpn3", // 组件里的data是一个函数,必须要返回一个对象,变量写在返回对象里 props: { ctitle: { //参数也使用对象形式,type传类型 default函数,是在找不到参数时返回一个值显示 type: String, default () { return {} } } } })
2. Then add the required attributes to the parent component
//父组件 const app = new Vue({ el: "#app", data: { //这里的title就是要传入子组件的属性 在父组件中定义 title: 'orange' }, methods: {}, })
3. Bind two parameters to the parent component
<div id="app"> //可以理解为将父组件中title 赋值给 ctitle,这样子组件就可以使用父组件的data属性了 <cpn3 :ctitle="title"></cpn3> </div>
4. Finally In the sub-component, use the property name defined by the sub-component
//子组件 <template id="cpn3"> <div> //这里的属性名为ctitle <h1 id="ctitle">{{ctitle}}</h1> </div> </template>
Vue Pass from son to parent
1. Pass from son to parent means,, sub-component The content is passed to the parent component, so that the parent component can use the data from the child component at any time. The usage method is: Custom function
#1. First, let’s make an imitation Taobao sidebar product. Click or move the mouse to the above to display the content product case
2. Taking the above picture as an example, let’s talk about the child-to-father transmission of components
2.1 Let’s separate the sub-component and the parent component. Let’s talk about them one by one. Let’s first look at the sub-component code
<!-- 子组件 --> <template id="cpn"> <div> <h1 id="ctitle">{{ctitle}}</h1> <button v-for="item in list" @click="goodsclick(item)">{{item.name}}</button> </div> </template> <script> Vue.component("cpn", { template: '#cpn', //父传子 props props: { ctitle: { type: String, //找不到数据时 default () { return {} } } }, data() { return { list: [{ id: 'phone', name: '手机' }, { id: 'tv', name: '电视' }, { id: 'p', name: '家电' }, { id: 'computer', name: '电脑' }, ] } }, //子传父 自定义事件 methods: { // 自定义事件 goodsclick(item) { this.$emit('itemclick', item) } } }) </script>
Explanation: The sub-component is to write a component (html) and wrap it up , and can be used at any time, which is equivalent to HTML, just packaged (this understanding should be no problem)
1. Subcomponents have data, methods, and attributes like vue. So write an array in data, and then use v-for to traverse and generate buttons There is no difference from normal writing, then write a function, bind the click event, and pass the item, which is the object passed in the array, into the function,
<button v-for="item in list" @click="goodsclick(item)">{{item.name}}</button>
2. Then the most important and critical step comes , that is, in the click event function you bound, send a custom function to the parent component, that is, send it to the parent component
methods: { // 自定义事件 goodsclick(item) { //itemclick就是自定义函数,并且将item也传过去给父组件 this.$emit('itemclick', item) } }
#This is when the child component is written, this It is the style of the child component, as shown in the picture above
2. Next, let’s talk about how the parent component receives the custom event from the child component
<!-- 父组件 --> <div id="app"> <cpn :ctitle="title" @itemclick="cpnclick"></cpn> </div> <script> //父组件 const app = new Vue({ el: "#app", data: { title: "title", }, methods: { cpnclick(item) { console.log("cpnclick", item); } } }) </script>
Explain: After you write the subcomponent, you need to call it. The call is just your component name as the label. Here is
<cpn :ctitle="title" @itemclick="cpnclick"></cpn>
Write a function in the parent component and bind the self passed from the subcomponent in it. Define the event, so that the sub-component is successfully bound, so that the sub-component transfers data to the parent component.
End of this chapter
For more programming-related knowledge, please visit:Introduction to Programming ! !
The above is the detailed content of How to communicate between parent and child components in Vue? Let's talk about the methods of passing on from father to son and from son to father.. For more information, please follow other related articles on the PHP Chinese website!

Vue.js improves user experience through multiple functions: 1. Responsive system realizes real-time data feedback; 2. Component development improves code reusability; 3. VueRouter provides smooth navigation; 4. Dynamic data binding and transition animation enhance interaction effect; 5. Error processing mechanism ensures user feedback; 6. Performance optimization and best practices improve application performance.

Vue.js' role in web development is to act as a progressive JavaScript framework that simplifies the development process and improves efficiency. 1) It enables developers to focus on business logic through responsive data binding and component development. 2) The working principle of Vue.js relies on responsive systems and virtual DOM to optimize performance. 3) In actual projects, it is common practice to use Vuex to manage global state and optimize data responsiveness.

Vue.js is a progressive JavaScript framework released by You Yuxi in 2014 to build a user interface. Its core advantages include: 1. Responsive data binding, automatic update view of data changes; 2. Component development, the UI can be split into independent and reusable components.

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

Netflix's choice in front-end technology mainly focuses on three aspects: performance optimization, scalability and user experience. 1. Performance optimization: Netflix chose React as the main framework and developed tools such as SpeedCurve and Boomerang to monitor and optimize the user experience. 2. Scalability: They adopt a micro front-end architecture, splitting applications into independent modules, improving development efficiency and system scalability. 3. User experience: Netflix uses the Material-UI component library to continuously optimize the interface through A/B testing and user feedback to ensure consistency and aesthetics.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

Netflix mainly considers performance, scalability, development efficiency, ecosystem, technical debt and maintenance costs in framework selection. 1. Performance and scalability: Java and SpringBoot are selected to efficiently process massive data and high concurrent requests. 2. Development efficiency and ecosystem: Use React to improve front-end development efficiency and utilize its rich ecosystem. 3. Technical debt and maintenance costs: Choose Node.js to build microservices to reduce maintenance costs and technical debt.

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)