广告
返回顶部
首页 > 资讯 > 前端开发 > node.js >Vue组件实例间直接访问的示例分析
  • 903
分享到

Vue组件实例间直接访问的示例分析

2024-04-02 19:04:59 903人浏览 泡泡鱼
摘要

小编给大家分享一下Vue组件实例间直接访问的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!前面的话  有时候需要父组件访

小编给大家分享一下Vue组件实例间直接访问的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

前面的话

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

$parent 

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

  下面是一个简易实例

<div id="example">
 <parent-component></parent-component>
</div>
<template id="parent-component">
 <div class="parent">
 <h4>我是父组件</h4>
 <input v-model="parentMsg">
 <p>{{parentMsg}}</p>
 <child-component></child-component> 
 </div>
</template>
<template id="child-component">
 <div class="child">
 <h4>我是子组件</h4>
 <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">
 <h4>我是根组件</h4>
 <input v-model="rootMsg">
 <p>{{rootMsg}}</p> 
 <parent-component></parent-component>
</div>
<template id="parent-component">
 <div class="parent">
 <h4>我是父组件</h4>
 <input v-model="parentMsg">
 <p>{{parentMsg}}</p>
 <child-component></child-component> 
 </div>
</template>
<template id="child-component">
 <div class="child">
 <h4>我是子组件</h4>
 <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">
 <h4>我是父组件</h4>
 <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">
 <h4>我是子组件1</h4>
 <input v-model="msg">
 <p>{{msg}}</p>
 </div>
</template>
<template id="child-component2">
 <div class="child">
 <h4>我是子组件2</h4>
 <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">
 <h4>我是父组件</h4>
 <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">
 <h4>我是子组件1</h4>
 <input v-model="msg">
 <p>{{msg}}</p>
 </div>
</template>
<template id="child-component2">
 <div class="child">
 <h4>我是子组件2</h4>
 <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组件实例间直接访问的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网node.js频道!

--结束END--

本文标题: Vue组件实例间直接访问的示例分析

本文链接: https://www.lsjlt.com/news/76357.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Vue组件实例间直接访问的示例分析
    小编给大家分享一下Vue组件实例间直接访问的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!前面的话  有时候需要父组件访...
    99+
    2022-10-19
  • Vue组件的示例分析
    这篇文章主要介绍了Vue组件的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Vue实例项目启动过程看一下现在我们的项目,想想整个项目...
    99+
    2022-10-19
  • Vue中组件的示例分析
    这篇文章将为大家详细讲解有关Vue中组件的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。<body>    <div id=&q...
    99+
    2023-06-25
  • vue组件之间数据传递的示例分析
    小编给大家分享一下vue组件之间数据传递的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!(1)props属性:在父组件中,可以通过子组件标签属性的形式将数据或者函数传给子组件,子组...
    99+
    2022-10-19
  • Vue组件通信的示例分析
    这篇文章主要介绍Vue组件通信的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!实践方法由于更换新的框架,我们的项目由Avalon更新成Vue.但是为了兼容以前的业务代码,不能...
    99+
    2022-10-19
  • vue组件传值的示例分析
    这篇文章主要为大家展示了“vue组件传值的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“vue组件传值的示例分析”这篇文章吧。前言vue中的组件传值大家应该都不陌生,今天用两个简单易懂的...
    99+
    2023-06-29
  • Vuejs 2.0中子组件访问/调用父组件的示例分析
    这篇文章给大家分享的是有关Vuejs 2.0中子组件访问/调用父组件的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。子组件:<template>  ...
    99+
    2022-10-19
  • Angualr组件间通信的示例分析
    这篇文章主要介绍Angualr组件间通信的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Angualr 组件间通信约定: 遵循Angular官方的说法,下文中的Angular...
    99+
    2022-10-19
  • vue中组件参数的示例分析
    这篇文章给大家分享的是有关vue中组件参数的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1.vue中组件参数我们可以为组件的 prop 指...
    99+
    2022-10-19
  • Vue函数式组件的示例分析
    这篇文章将为大家详细讲解有关Vue函数式组件的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。函数式组件特点:没有管理任何状态没有监听任何传递给它的状态没有生命周期...
    99+
    2022-10-19
  • vue组件与复用的示例分析
    这篇文章给大家分享的是有关vue组件与复用的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、什么是组件组件 (Component) 是 Vue.js 最强大的功能之一。...
    99+
    2022-10-19
  • vue可重用组件的示例分析
    这篇文章给大家分享的是有关vue可重用组件的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。可重用组件不是你想的那样可重用组件不一定是大的或复杂的东西,我经常使小而短的组件可重复使用。因为我不会到处重写这段...
    99+
    2023-06-27
  • vue组件化的实例分析
    这篇文章将为大家详细讲解有关vue组件化的实例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。全局组件<!DOCTYPE html> <ht...
    99+
    2022-10-19
  • Vue实现组件间通信的示例
    这篇文章主要介绍了Vue实现组件间通信的示例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1. 父子间通信最常见的就是父子之间的通信,通信是双向的数据传递。1.1 父组件 -...
    99+
    2023-06-15
  • Vue组件间的双向绑定示例解析
    目录何为组件间双向绑定示例解析总结何为组件间双向绑定 我们都知道当父组件改变了某个值后,如果这个值传给了子组件,那么子组件也会自动跟着改变,但是这是单向的,使用v-bind的方式,即...
    99+
    2023-03-19
    Vue组件间双向绑定 Vue组件绑定
  • Vue中函数化组件的示例分析
    这篇文章将为大家详细讲解有关Vue中函数化组件的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。介绍之前创建的锚点标题组件是比较简单,没有管理或者监听任何传递给他的...
    99+
    2022-10-19
  • vue组件三大核心的示例分析
    这篇文章主要介绍了vue组件三大核心的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一、属性 1.自定义属性propsprop 定义...
    99+
    2022-10-19
  • Vue组件单元测试的示例分析
    这篇文章将为大家详细讲解有关Vue组件单元测试的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。关于 Vue 组件单元测试最常见的问题就是“我究竟应该测试什么?”虽...
    99+
    2022-10-19
  • vue中组件元数据的示例分析
    这篇文章将为大家详细讲解有关vue中组件元数据的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。组件元数据并不是你添加到组件的每一点信息都是状态。有时你需要添加一些元数据来为其他组件提供更多信息。例...
    99+
    2023-06-27
  • vue.js实例对象+组件树的示例分析
    这篇文章将为大家详细讲解有关vue.js实例对象+组件树的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。vue的实例对象首先用js的new关键字实例化一个vuee...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作