全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Vue组件实例间的直接访问实现代码

前面的话

  有时候需要父组件访问子组件,子组件访问父组件,或者是子组件访问根组件。 在组件实例中,Vue提供了相应的属性,包括$parent、$children、$refs和$root,这些属性都挂载在组件的this上。本文将详细介绍Vue组件实例间的直接访问

$parent 

  $parent表示父组件的实例,该属性只读

  下面是一个简易实例

<div id="example">
 <parent-component></parent-component>
</div>
<template id="parent-component">
 <div class="parent">
 <h3>我是父组件</h3>
 <input v-model="parentMsg">
 <p>{{parentMsg}}</p>
 <child-component></child-component> 
 </div>
</template>
<template id="child-component">
 <div class="child">
 <h3>我是子组件</h3>
 <p>{{msg}}</p>
 <button v-on:click="showData">显示父组件数据</button> 
 </div>
</template>
<script>
// 注册
Vue.component('parent-component', {
 template: '#parent-component',
 data(){
 return{
  parentMsg:'我是父组件的数据'
 }
 },
 components:{
 'child-component':{
  template:'#child-component',
  data(){
  return{
   msg:''
  }
  },
  methods:{
  showData(){
   this.msg = this.$parent.parentMsg;
  }
  }
 }
 }
})
// 创建根实例
new Vue({
 el: '#example'
})
</script>

$root 

  $root表示当前组件树的根 Vue 实例。如果当前实例没有父实例,此实例将会是其自己。该属性只读

<div id="example">
 <h3>我是根组件</h3>
 <input v-model="rootMsg">
 <p>{{rootMsg}}</p> 
 <parent-component></parent-component>
</div>
<template id="parent-component">
 <div class="parent">
 <h3>我是父组件</h3>
 <input v-model="parentMsg">
 <p>{{parentMsg}}</p>
 <child-component></child-component> 
 </div>
</template>
<template id="child-component">
 <div class="child">
 <h3>我是子组件</h3>
 <p>
  <button v-on:click="showRootData">显示根组件数据</button><span>{{rootMsg}}</span>
 </p>  
 <p>
  <button v-on:click="showParentData">显示父组件数据</button><span>{{parentMsg}}</span>
 </p>
 </div>
</template>
<script>
// 注册
Vue.component('parent-component', {
 template: '#parent-component',
 data(){
 return{
  parentMsg:'我是父组件的数据'
 }
 },
 components:{
 'child-component':{
  template:'#child-component',
  data(){
  return{
   parentMsg:'',
   rootMsg:''
  }
  },
  methods:{
  showParentData(){
   this.parentMsg = this.$parent.parentMsg;
  },
  showRootData(){
   this.rootMsg = this.$root.rootMsg;
  },  
  }
 }
 }
})
// 创建根实例
new Vue({
 el: '#example',
 data:{
 rootMsg:'我是根组件数据'
 }
})
</script>

$children 

  $children表示当前实例的直接子组件。需要注意$children并不保证顺序,也不是响应式的。如果正在尝试使用$children来进行数据绑定,考虑使用一个数组配合v-for来生成子组件,并且使用Array作为真正的来源

<div id="example">
 <parent-component></parent-component>
</div>
<template id="parent-component">
 <div class="parent">
 <h3>我是父组件</h3>
 <button @click="getData">获取子组件数据</button>
 <br>
 <div v-html="msg"></div>
 <child-component1></child-component1> 
 <child-component2></child-component2> 
 </div>
</template>
<template id="child-component1">
 <div class="child">
 <h3>我是子组件1</h3>
 <input v-model="msg">
 <p>{{msg}}</p>
 </div>
</template>
<template id="child-component2">
 <div class="child">
 <h3>我是子组件2</h3>
 <input v-model="msg">
 <p>{{msg}}</p>
 </div>
</template>
<script>
// 注册
Vue.component('parent-component', {
 template: '#parent-component',
 data(){
 return{
  msg:'',
 }
 },
 methods:{
 getData(){
  let html = '';
  let children = this.$children;
  for(var i = 0; i < children.length;i++){
  html+= '<div>' + children[i].msg + '</div>';
  }
  this.msg = html;
 }
 },
 components:{
 'child-component1':{
  template:'#child-component1',
  data(){
  return{
   msg:'',
  }
  },
 },
 'child-component2':{
  template:'#child-component2',
  data(){
  return{
   msg:'',
  }
  },
 }, 
 } 
})
// 创建根实例
new Vue({
 el: '#example',
})
</script>

$refs

  组件个数较多时,难以记住各个组件的顺序和位置,通过序号访问子组件不是很方便

  在子组件上使用ref属性,可以给子组件指定一个索引ID:

<child-component1 ref="c1"></child-component1>
<child-component2 ref="c2"></child-component2>

  在父组件中,则通过$refs.索引ID访问子组件的实例

this.$refs.c1

this.$refs.c2

<div id="example">
 <parent-component></parent-component>
</div>
<template id="parent-component">
 <div class="parent">
 <h3>我是父组件</h3>
 <div>
  <button @click="getData1">获取子组件c1的数据</button>
  <p>{{msg1}}</p>
 </div>
 <div>
  <button @click="getData2">获取子组件c2的数据</button>
  <p>{{msg2}}</p>
 </div>
 <child-component1 ref="c1"></child-component1> 
 <child-component2 ref="c2"></child-component2> 
 </div>
</template>
<template id="child-component1">
 <div class="child">
 <h3>我是子组件1</h3>
 <input v-model="msg">
 <p>{{msg}}</p>
 </div>
</template>
<template id="child-component2">
 <div class="child">
 <h3>我是子组件2</h3>
 <input v-model="msg">
 <p>{{msg}}</p>
 </div>
</template>
<script>
// 注册
Vue.component('parent-component', {
 template: '#parent-component',
 data(){
 return{
  msg1:'',
  msg2:'',
 }
 },
 methods:{
 getData1(){
  this.msg1 = this.$refs.c1.msg;
 },
 getData2(){
  this.msg2 = this.$refs.c2.msg;
 }, 
 },
 components:{
 'child-component1':{
  template:'#child-component1',
  data(){
  return{
   msg:'',
  }
  },
 },
 'child-component2':{
  template:'#child-component2',
  data(){
  return{
   msg:'',
  }
  },
 }, 
 } 
})
// 创建根实例
new Vue({
 el: '#example',
})
</script>

总结 

  虽然vue提供了以上方式对组件实例进行直接访问,但并不推荐这么做。这会导致组件间紧密耦合,且自身状态难以理解,所以尽量使用props、自定义事件以及内容分发slot来传递数据。


# Vue组件实例  # vue  # 组件访问  # Vue.js 递归组件实现树形菜单(实例分享)  # Vue.js组件使用开发实例教程  # vue.js表格组件开发的实例详解  # Vuejs第十一篇组件之slot内容分发实例详解  # Vue.js 父子组件通讯开发实例  # Vuejs第九篇之组件作用域及props数据传递实例详解  # Vuejs第八篇之Vuejs组件的定义实例解析  # vue分页组件table-pagebar使用实例解析  # 我是  # 是一个  # 将会  # 或者是  # 自定义  # 详细介绍  # 这么做  # 很方便  # 这会  # 绑定  # 需要注意  # lt  # parentMsg  # js  # component  # brush  # model  # gt  # template  # div 


相关文章: 无锡制作网站公司有哪些,无锡优八网络科技有限公司介绍?  宝华建站服务条款解析:五站合一功能与SEO优化设置指南  中山网站制作网页,中山新生登记系统登记流程?  做企业网站制作流程,企业网站制作基本流程有哪些?  详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)  营销式网站制作方案,销售哪个网站招聘效果最好?  百度网页制作网站有哪些,谁能告诉我百度网站是怎么联系?  定制建站策划方案_专业建站与网站建设方案一站式指南  齐河建站公司:营销型网站建设与SEO优化双核驱动策略  如何通过VPS建站无需域名直接访问?  品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?  建站VPS配置与SEO优化指南:关键词排名提升策略  网站制作壁纸教程视频,电脑壁纸网站?  内部网站制作流程,如何建立公司内部网站?  制作网站建设的公司有哪些,网站建设比较好的公司都有哪些?  如何破解联通资金短缺导致的基站建设难题?  如何选择建站程序?包含哪些必备功能与类型?  公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?  建站之星各版本价格是多少?  公司网站制作需要多少钱,找人做公司网站需要多少钱?  c# 在高并发场景下,委托和接口调用的性能对比  建站之星如何开启自定义404页面避免用户流失?  建站主机SSH密钥生成步骤及常见问题解答?  南宁网站建设制作定制,南宁网站建设可以定制吗?  建站主机选择指南:服务器配置与SEO优化实战技巧  常州自助建站工具推荐:低成本搭建与模板选择技巧  javascript中对象的定义、使用以及对象和原型链操作小结  广州商城建站系统开发成本与周期如何控制?  建站之星后台密码遗忘或太弱?如何重置与强化?  c++23 std::expected怎么用 c++优雅处理函数错误返回【详解】  网站规划与制作是什么,电子商务网站系统规划的内容及步骤是什么?  开源网站制作软件,开源网站什么意思?  如何快速建站并高效导出源代码?  个人摄影网站制作流程,摄影爱好者都去什么网站?  建站主机选购指南:核心配置优化与品牌推荐方案  红河网站制作公司,红河事业单位身份证如何上传?  Android自定义控件实现温度旋转按钮效果  哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?  如何通过商城免费建站系统源码自定义网站主题?  制作网站的模板软件,网站怎么建设?  教学网站制作软件,学习*后期制作的网站有哪些?  广东企业建站网站优化与SEO营销核心策略指南  建站之家VIP精选网站模板与SEO优化教程整合指南  已有域名如何快速搭建专属网站?  小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化  如何用美橙互联一键搭建多站合一网站?  网站制作免费,什么网站能看正片电影?  ui设计制作网站有哪些,手机UI设计网址吗?  如何通过虚拟机搭建网站?详细步骤解析  如何选择美橙互联多站合一建站方案? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。