


How to communicate between Vue parent and child components? (with examples)
vueHow to communicate between parent and child components? The following article will introduce to you how the parent component passes data to the child component, and the child component passes the data to the parent component. I hope it will be helpful to you!
The variables and data in each component are independent. What should we do if another component wants to access the data in another component?
How to solve the communication between components?
Solution:
You can use the parent component to pass data to the child component, and you can also use the child component to pass data to the parent component. In short, it is passed down from father to son, and from son to father. (Learning video sharing: vue video tutorial)
Let’s talk in detail about how the parent component passes data to the child component.
father-to-child
Theory: If a component A introduces and uses another component B, then component A is the parent component and component B is Subassembly.
Implementation process:
1.在父组件中引入子组件、注册子组件、使用子组件 2.在父组件中的子组件标签上自定义一个属性 左边是定义的名称,右边是父组件中的数据 例如 <MyCon :list="list" /> 3.在子组件中用prpos接收父组件传来的数据 例如:prpos:['list'] 注意这里面的名称必须要和父组件 定义的名称一致才能可以。
Principle illustration
Parent component Footer.vue
Customize a property on the child component label in the parent component
<template> <div> <h1 id="父组件传子组件">父组件传子组件</h1> <!-- 使用组件 自定义属性 --> <MyCon :name="name" :age="age" /> </div> </template> <script> // 引入组件 --> 创建组件 --> 使用组件 // 引入组件 import MyCon from "./MyCon.vue"; export default { // 创建组件 components: { MyCon }, // 数据 data() { return { name: "张三", age: 38, }; }, }; </script>
Child component MyCon.vue
Use prpos in the child component to receive data from the parent component
<template> <div> <h2 id="子组件">子组件</h2> // 直接在标签中使用 <p>{{ name }} {{ age }}</p> <button @click="fn">点击修改props的值</button> </div> </template> <script> export default { // 子组件接收父组件传来的数据 props: ["name", "age"], methods: { fn() { this.name = "傻逼谭磊"; this.age = 20 }, }, }; </script>
The small case uses parent-to-child transmission
Parent component App.vue
<template> <div style="border: 1px solid #ccc; margin: 5px; padding: 5px"> <h1 id="父组件">父组件</h1> <!-- 1. 父传。自定义属性 --> <MyProduct v-for="item in list" :key="item.id" :price="item.proprice" :info="item.info" :goodname="item.proname" /> </div> </template> <script> // 导入->注册->使用 import MyProduct from "./MyProduct.vue"; export default { data() { return { list: [ { id: 1, proname: "超级好吃的棒棒糖", proprice: 18.8, info: "开业大酬宾, 全场8折", }, { id: 2, proname: "超级好吃的大鸡腿", proprice: 34.2, info: "好吃不腻, 快来买啊", }, { id: 3, proname: "超级无敌的冰激凌", proprice: 14.2, info: "炎热的夏天, 来个冰激凌了", }, ], }; }, components: { MyProduct }, }; </script> <style> </style>
Child component MyProduct.vue
<template> <div style="border: 1px solid #ccc; margin: 5px; padding: 5px"> <h2 id="标题-nbsp-nbsp-goodname-nbsp">标题: {{ goodname }}</h2> <p>价格: {{ price }}元</p> <p>{{ info }}</p> </div> </template> <script> export default { props: ["goodname", "price", "info"], }; </script> <style> </style>
Effect display
Son passes on to father
Implementation process
1.在父组件中引入子组件、注册子组件、使用子组件 2.在父组件的子组件标签上加一个事件监听 例如: <MyCon @abc="fn" /> 3.在子组件中触发这个自定义的监听事件。例如:this.$emit("abc",参数)
Principle illustration
Parent component App.vue
Add an event listener on the sub-component label of the parent component and receive it with formal parameters Data
<template> <div> <h1 id="父组件">父组件</h1> <!-- 1.添加事件监听 --> <!-- 当子组件发生了abc事件要执行fn函数 --> <MyCon @abc="fn" /> </div> </template> <script> // 引入子组件 import MyCon from "./MyCon.vue"; export default { methods: { // 形参接收 fn(obj) { // 打印查看有没有获取到 console.log("fn发生了abc事件", obj); }, }, components: { MyCon }, }; </script>
Subcomponent MyCon.vue
Trigger this custom listening event in the subcomponent
<template> <div> <h2 id="子组件">子组件</h2> <button @click="ConFn">子传父</button> </div> </template> <script> export default { methods: { ConFn() { console.log("子组件click"); // 2.触发abc事件 this.$emit("abc", { name: "吊毛谭磊" }); }, }, }; </script>
Product The case uses child to parent
parent component App.vue
<template> <div style="border: 1px solid #ccc; margin: 5px; padding: 5px"> <h1 id="父组件">父组件</h1> <!-- 1. 父传。自定义属性 --> <!-- 添加自定义事件 --> <MyProduct v-for="(item, idx) in list" :idx="idx" :key="item.id" :price="item.proprice" :info="item.info" :goodname="item.proname" @pdd="fn" /> </div> </template> <script> // 导入->注册->使用 import MyProduct from "./MyProduct.vue"; export default { data() { return { list: [ { id: 1, proname: "超级好吃的棒棒糖", proprice: 18.8, info: "开业大酬宾, 全场8折", }, { id: 2, proname: "超级好吃的大鸡腿", proprice: 34.2, info: "好吃不腻, 快来买啊", }, { id: 3, proname: "超级无敌的冰激凌", proprice: 14.2, info: "炎热的夏天, 来个冰激凌了", }, ], }; }, components: { MyProduct }, methods: { fn(obj) { console.log("父组件,收到了pdd事件", obj); // 计算砍了几元 相减 再进行重新赋值 this.list[obj.idx].proprice -= obj.ran; }, }, }; </script> <style> </style>
child component MyProduct.vue
<template> <div style="border: 1px solid #ccc; margin: 5px; padding: 5px"> <h2 id="标题-nbsp-nbsp-goodname-nbsp">标题: {{ goodname }}</h2> <p>价格: {{ price }}元</p> <p>{{ info }}</p> <button @click="bargain">随机砍价</button> </div> </template> <script> export default { // 子接收 props: ["goodname", "price", "info", "idx"], methods: { bargain() { // 随机数字 const ran = Math.ceil(Math.random() * 10); // 触发自定义事件 // 为了知道具体是哪个商品要减少价格,所以要回传idx下标 this.$emit("pdd", { idx: this.idx, ran }); }, }, }; </script> <style> </style>
Effect display
(Learning video sharing: web front-end development, Basic programming video)
The above is the detailed content of How to communicate between Vue parent and child components? (with examples). For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the <script> tag in the HTML file that refers to the Vue file.

Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() => { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.
