search
HomeWeb Front-endJS TutorialDetailed explanation of parent-child communication between Vue components

This article mainly introduces the relevant information on the communication between the father and son of the Vue component in detail. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

3. Communication between components ($parent $refs)

If the parent component wants to obtain the data of the child component:

①In When calling the child component, specify the ref attribute

##

② Find the instance object of the subcomponent based on the name of the specified reference

this.$refs.mySon

If the subcomponent wants to get the data of the parent component:


①Read directly


this.$parent
Get the data of the subcomponent through this.$refs

Code:


<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>组件间通信-01</title>
  <script src="js/vue.js"></script>
 </head>
 <body>
 <p id="container">
    <p>{{msg}}</p>
    <dahua></dahua>
  </p>
  <script>
  //vue提供的ref
    Vue.component("dahua",{
      data:function(){
        return{
          mySonName:""
        }
      },
      methods:{
      //通过$refs拿到指定的所引用的对应的组件的实例对象
        getSonName:function(){
          this.mySonName = this.$refs.mySon.name;
        }
      },
      template:`
        <p>
          <h1 id="这是父组件">这是父组件</h1>
          <button @click = "getSonName">获取子组件数据</button>
          <span>{{mySonName}}</span>
          <hr>
          <xiaohua ref="mySon"></xiaohua>
        </p>
      `
    })
//  创建子组件
    Vue.component("xiaohua",{
      data:function(){
        return{
          name:"小花"
        }
      },
      template:`
          <h1 id="这是子组件">这是子组件</h1>
      `
    })
    new Vue({
      el:"#container",
      data:{
        msg:"Hello VueJs"
      }
    })
  </script>
 </body>
</html>

The child component obtains the data of the parent component through $parent


<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>组件间通信-02</title>
  <script src="js/vue.js"></script>
 </head>
 <body>
 <p id="container">
    <p>{{msg}}</p>
    <dahua></dahua>
  </p>
  <script>
    //创建子组件
    Vue.component("dahua",{
      data:function(){
        return{
          myName:"大花"
        }
      },
      template:`
        <p>
          <h1 id="这是父组件">这是父组件</h1>
          <hr>
          <xiaohua></xiaohua>
        </p>
      `
    })
    //创建子组件
    Vue.component("xiaohua",{
      data:function(){
        return{
          msg:""
        }
      },
      template:`
        <p>
            <h1 id="这是子组件">这是子组件</h1>
            <p>{{msg}}</p>
        </p>
      `,
      created:function(){
        //在子组件创建完成时获取父组件的数据
        //保存在msg中在p标签中显示
          this.msg = this.$parent.myName;
      }
    })
    new Vue({
      el:"#container",
      data:{
        msg:"Hello VueJs"
      }
    })
  </script>
 </body>
</html>

Related recommendations:


PHP handles parent-child level selection problem

react.js Parent-child component data binding real-time communication example display

array sorting method of parent-child list solution Idea

The above is the detailed content of Detailed explanation of parent-child communication between Vue components. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Vue与服务器端通信的刨析:如何处理断网情况Vue与服务器端通信的刨析:如何处理断网情况Aug 10, 2023 am 10:55 AM

Vue与服务器端通信的探析:处理断网情况的策略引言:在现代Web开发中,Vue.js已成为一种广泛使用的前端框架。然而,由于网络环境的不稳定性,处理断网情况是一个需要我们考虑的重要问题。本文将分析如何在Vue中处理断网情况,并给出相应的代码示例。一、断网情况分析在网络状况较好的情况下,Vue可以通过Ajax请求或WebSocket与服务器进行通信。但是,

如何通过PHP与P2P协议实现点对点通信如何通过PHP与P2P协议实现点对点通信Jul 28, 2023 pm 10:13 PM

如何通过PHP与P2P协议实现点对点通信随着互联网的发展,点对点(peer-to-peer,简称P2P)通信逐渐成为一种重要的通信方式。与传统的客户端-服务器通信方式相比,P2P通信具有更好的稳定性和伸缩性。在本文中,我们将介绍如何使用PHP与P2P协议实现点对点通信,并提供相应的代码示例。首先,我们需要了解P2P通信的基本原理。P2P协议允许多台计算机直接

诺基亚计划以 1.85 亿欧元的价格出售其设备管理和服务管理平台业务诺基亚计划以 1.85 亿欧元的价格出售其设备管理和服务管理平台业务Dec 21, 2023 am 08:07 AM

诺基亚今日宣布,将其设备管理和服务管理平台业务以1.85亿欧元的价格出售给Lumine集团,预计明年第一季度完成根据我们的调查发现,Lumine是一家通信和媒体软件公司,最近从ConstellationSoftware分拆出来。作为交易的一部分,预计会有大约500名诺基亚员工加入Lumine据公开资料显示,这些平台的业务主要是诺基亚通过之前两次收购Motive和mFormation形成的。Lumine称其有意恢复Motive品牌,并将其作为一个独立的业务部门Lumine表示,收购价格包括一笔高达

Vue组件通信:使用$destroy进行组件销毁通信Vue组件通信:使用$destroy进行组件销毁通信Jul 09, 2023 pm 07:52 PM

Vue组件通信:使用$destroy进行组件销毁通信在Vue开发中,组件通信是非常重要的一个方面。Vue提供了多种方式来实现组件通信,比如props和emit、vuex等。本文将介绍另一种组件通信方式:使用$destroy进行组件销毁通信。在Vue中,每个组件都有一个生命周期,其中包含了一系列的生命周期钩子函数。组件的销毁也是其中之一,Vue提供了一个$de

如何使用Swoole实现WebSocket通信如何使用Swoole实现WebSocket通信Nov 07, 2023 pm 12:56 PM

Swoole是一个高性能的PHP协程网络框架,支持异步IO、多进程、多线程、协程等特性。其中,Swoole提供的WebSocket组件可用于实现实时双向通信,是构建实时应用的理想选择。本文将介绍如何使用Swoole实现WebSocket通信,并提供具体的代码示例。一、环境准备在使用Swoole实现WebSocket通信前,需要确保已安装Swoole扩展。可通

串行通信和并行通信的区别是什么串行通信和并行通信的区别是什么May 16, 2023 am 11:44 AM

串行通信和并行通信的区别:1、并行通信指的是并行通信端口,同时传送八路信号,一次并行传送完整的一个字节信息,串行通信指的是串行通信端口, 在一个方向上只能传送一路信号,传送一个字节信息时,只能一位一位地依次传送;2、并行通信是在同一时刻发送多位数据,串行通信用一根线在不同的时刻发送8位数据;3、并行通信发送速度快,距离短资源占用多,串行通信发送速度慢,距离远资源占用少。

5G通信到来,但是5G体验真的完全超越4G吗?5G通信到来,但是5G体验真的完全超越4G吗?Jan 08, 2024 pm 10:30 PM

28日,2023上海世界移动通信大会(MWC2023上海)开幕,“5.5G”成为热门主题,华为副董事长、轮值董事长、CFO孟晚舟在大会上也发表了“拥抱5G变革”的主题演讲,她认为5.5G是5G网络演进的必然之路。“5.5G网络下行万兆、上行千兆、千亿联接、内生智能的网络特征已经明确,从5G到5.5G,将更好地匹配人联、物联、感知、高端制造等场景,孵化更多的商业新机会。”对用户来说,5.5G到底意味着什么?我们还不知道。当行业已在讨论5.5G时?早已普及的5G体验到底如何?5G体验争议:真的比4G

数据通信中的信道传输速率单位是bps,它表示什么数据通信中的信道传输速率单位是bps,它表示什么Jan 18, 2021 pm 02:58 PM

数据通信中的信道传输速率单位是bps,它表示“位/秒”或“比特/秒”,即数据传输速率在数值上等于每秒钟传输构成数据代码的二进制比特数,也称“比特率”。比特率表示单位时间内传送比特的数目,用于衡量数字信息的传送速度;根据每帧图像存储时所占的比特数和传输比特率,可以计算数字图像信息传输的速度。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft