


Detailed example of communication between vue components Detailed explanation of child and parent (2)
Continue learning with a detailed explanation of the communication between the parent and child of the vue component. This article mainly introduces in detail the information related to the communication between children and parents between Vue components. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
2. Communication between components (child components pass values to parent components)
Complete data transmission through events.
①Define a method in the parent component to receive the value passed by the child component through the event
##
methods:{ recvMsg:function(msg){ //参数msg就是子组件通过事件出来的数据 } }②Bind event processing function
<child-component @myEvent="recvMsg"></child-component>③Trigger events in child components
事件名,值 this.$emit('myEvent',myPhone) //触发一个叫做myEvent的事件,同时把第二个参数数据传递给事件对应的处理函数
Summary:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>组件间通信子传父</title> <script src="js/vue.js"></script> </head> <body> <p id="container"> <p>{{msg}}</p> <parent-component></parent-component> </p> <script> //通过事件的方式传递 // 绑定 -- 触发 Vue.component("parent-component",{ data:function(){ return { sonMsg:"" } }, methods:{ //msg参数要拿子传递的值 recvMsg:function(msg){ console.log("父组件接收到子组件的数据"+msg); this.sonMsg = msg; } }, template:` <p> <h1 id="这是父组件">这是父组件</h1> <p>子组件传来的数据为:{{sonMsg}}</p> <hr/> <child-component @customEvent="recvMsg"></child-component> </p> ` }) Vue.component("child-component",{ methods:{ sendMsg:function(){ //来触发绑定给子组件的自定义方法 //this.$emit("customEvent");第一个参数触发 //this.$emit("customEvent");第二个参数传值 this.$emit("customEvent","哈哈哈哈"); }, }, template:` <p> <h1 id="这是子组件">这是子组件</h1> <button @click="sendMsg">senToFather</button> </p> ` }) new Vue({ el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>Put an input in the child component and click the button to send the content entered by the user to the parent component
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>子与父之间的通信</title> <script src="js/vue.js"></script> </head> <body> <p id="container"> <p>{{msg}}</p> <parent-component></parent-component> </p> <script> //创建父组件 Vue.component("parent-component",{ //data属性 data:function(){ return{ sonMsg:"" } }, methods:{ recvMsg:function(msg){ this.sonMsg = msg } }, template:` <p> <h1 id="父组件">父组件</h1> <h4 id="子组件传递的数据-sonMsg">子组件传递的数据:{{sonMsg}}</h4> <child-component @customEvent="recvMsg"></child-component> </p> ` }) //创建子组件 Vue.component("child-component",{ data:function(){ return { myInput:"" } }, methods:{ sendMsg:function(){ this.$emit("customEvent",this.myInput); } }, template:` <p> <h1 id="子组件">子组件</h1> <input type="text" v-model="myInput"/> <button @click="sendMsg">发送</button> </p> ` }) new Vue({ el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>Related recommendations:
Detailed explanation of parent-child communication in the vue component (1)
Introduction and use of the v for directive in the vue component v- Analysis of alarm issues when for
Examples of methods for using iframe elements in vue components
The above is the detailed content of Detailed example of communication between vue components Detailed explanation of child and parent (2). For more information, please follow other related articles on the PHP Chinese website!

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.


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 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
