vue 的组件之间传值(父子组件,兄弟组件,祖孙组件,任意组件)

本文主要是介绍vue 的组件之间传值(父子组件,兄弟组件,祖孙组件,任意组件),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. props / $emit

  • props 父组件传值给子组件
  • $emit 子组件传值给父组件

props:

// 父组件
<template><div><Child v-bind:list="users"></Child></div> 
</template>
<script>import Child from "./components/Child" //子组件export default {name: 'App',components:{Child},data(){return{users:["Eric","Andy","Sai"]             }}}   }
</script>// 子组件
<template><ul><li v-for="item in list">{{ item }}</li></ul>
</template>
<script>export default {name: 'child',props:{list:{           //这个就是父组件中子标签自定义名字type:Array,     //对传递过来的值进行校验required:true   //必添}}}
</script>

$emit:

// 子组件
<template><div><h1 @click="changeTitle">{{ title }}</h1> //绑定一个点击事件</div>
</template>
<script>export default {name: 'header',data() {return {title:"子组件title"}},methods:{changeTitle() {this.$emit("titleChanged", "子向父组件传值")}}}
</script>// 父组件
<template><div><header v-on:titleChanged="updateTitle"></header><h2>{{ title }}</h2></div>
</template>
<script>
import Header from "./components/Header"export default {name: 'App',components:{ Header },data(){return{title:"传递的是一个值"}},methods:{updateTitle(e){   //声明这个函数this.title = e;}}}
</script>

2. $attrs / $listeners

  • $attrs:包含了父作用域中不被 Prop 所识别的特性绑定(class 和 style 除外),当一个组件没有申明任何 prop 时,这里会包含所有父作用域的绑定(class 和 style 除外),并且可以通过 v-bind="$attrs" 传入内部组件
  • $listeners:包含了父作用域中的 v-on 事件监听器(不包含 .native 修饰器的),可以通过 v-on="$listeners" 传入内部组件

$attrs与$listeners 是两个对象,$attrs 里存放的是父组件中绑定的非 Props 属性, $listeners里存放的是父组件中绑定的非原生事件。

// childCom1
<template><div class="border"><h2>childCom1</h2><child-com2 title="前端工匠" :aoo="aoo" :boo="boo" :coo="coo":style="{background: '#f2f2f2'}" class="box"@msgChanged="updateMsg"></child-com2><p>{{msg}}</p></div>
</template><script>
import ChildCom2 from '@/views/childCom2.vue'
export default {name: 'childCom1',components: { ChildCom2 },data () {return {aoo: 'Html',boo: 'CSS',coo: 'Javascript',msg: ''}},methods: {updateMsg (msg) {this.msg = msg}}
}
</script>
// childCom2
<template><div class="border"><h2>childCom2</h2><p>childCom2的$attrs: {{ $attrs }}</p><child-com3 v-bind="$attrs" v-on="$listeners"></child-com3></div>
</template>
<script>
const childCom3 = () => import('./childCom3.vue')
export default {name: 'childCom2',components: {childCom3},inheritAttrs: false,props: {aoo: String}
}
</script>

注意: 这里是 v-bind= 和 v-on=。 这里v-on是等号。

// childCom3
<template><div class="border"><h2>childCom3</h2><p @click="changeMsg">childCom3的$attrs: {{ $attrs }}</p></div>
</template>
<script>
export default {name: 'childCom3',inheritAttrs: true,props: {coo: String,title: String},methods: {changeMsg () {this.$emit('msgChanged', '我是childCom3的数据')}}
}
</script>

childCom2 中,$attrs 里没有 style 和 class 的内容。因为 props 中申明了 aoo ,所以也没有 aoo 。

关于 inheriAttrs 的作用

childCom2 中 inheriAttrs:false,childCom3 中 inheriAttrs:true。渲染 DOM 如下:

可以看到为 true 时,childCom3 组件中的根节点上绑定了 $attrs 的属性

3. provide / inject

  • provide 在祖先中提供变量
  • inject 在后代中注入变量

provide / inject 只能用于父传子,反向不能传递。

// grandpa.vue
export default {name: 'grandpa',// provide: {//   name: '前端工匠'// },provide () {return {name: '前端工匠',num: this.count}},data () {return {count: 3}}
}// grandson.vue
export default {name: 'grandson',inject: ['name', 'num'],created () {console.log(this.name)console.log(this.num)}
}

此时传递的数据,不是响应式的。grandpa 中修改 count 的值,grandson 中的 num 并不会改变。

实现响应式的两种方法:

  • provide祖先组件的实例,然后在子孙组件中注入依赖,这样就可以在子孙组件中直接获取或者修改祖先组件的实例的属性,不过这种方法有个缺点就是这个实例上挂载很多没有必要的东西比如props, methods。
  • Vue.observable 优化响应式 provide

第一种方法示例如下:

// grandpa.vue
export default {name: 'grandpa',// provide () {//   return {//     name: '前端工匠',//     num: this.count//   }// },provide () {return {grandpaInfo: this}},data () {return {name: '前端工匠',count: 3}},methods: {changeNum () {this.count++}}
}
// grandson.vue
export default {name: 'grandson',// inject: ['name', 'num'],inject: ['grandpaInfo'],created () {// console.log(this.name)// console.log(this.num)console.log(this.grandpaInfo.name)console.log(this.grandpaInfo.count)}
}

此时 grandpa 中的count 改变,grandson 中的 count 也会跟着变化

inject 也可以对祖先传过来的实例重新命名:

export default {name: 'grandson',// inject: ['grandpaInfo'],inject: {grandpaInfoObj: {from: 'grandpaInfo',default () {return {}}}},created () {// console.log(this.grandpaInfo.name)// console.log(this.grandpaInfo.count)console.log(this.grandpaInfoObj.name)console.log(this.grandpaInfoObj.count)}
}

第二种方法示例如下:

// grandpa.vue
import Vue from 'vue'
export default {name: 'grandpa',provide () {this.grandpaInfo = Vue.observable({name: '前端工匠',count: 3})return {grandpaInfo: this.grandpaInfo}}
}
// grandson.vue
export default {name: 'grandson',inject: ['grandpaInfo'],created () {console.log(this.grandpaInfo.name)console.log(this.grandpaInfo.count)}
}

此时 grandpa 中修改 this.grandpaInfo 中的值,grandson 中的值跟着改变。

4. $parent / $children

  • $parent 访问父实例
  • $children 访问子实例

// parent.vue
<script>
import Child from './child.vue'
export default {name: 'parent',components: { Child },data () {return {title: '我是父组件',childTitle: ''}},methods: {sayHello () {console.log('hello')},getChild () {this.childTitle = this.$children[0].titlethis.$children[0].sayBye()}}
}
</script>
// child.vue
<script>
export default {name: 'child',data () {return {title: '我是子组件',parentTitle: ''}},methods: {sayBye () {console.log('bye')},getParent () {this.parentTitle = this.$parent.titlethis.$parent.sayHello()}}
}
</script>

使用 $parent 和 $children 时,需要注意以下几点:

  • 使用 $parent 时,注意一些UI框架的组件嵌套。比如页面中如果 child 组件的外层有 el-row 包裹,那 child 的$parent 是 el-row 实例,需继续向上查找,才是 parent 组件实例。
  • 使用 $children 时,注意 children 的下标,是页面最终呈现时的下标。比如,如果 child 组件上有一个动态组件,只有某种情况下才显示,平时不显示。那么当动态组件显示时,child 的下标就会发生变化。

5.事件总线 eventbus ,$emit / $on

创建 bus 文件(新建js文件)

import Vue from 'vue'
export defult new Vue

使用:使用 bus 的组件,必须引入 bus 文件

// 组件 aa ,发送数据
<template><div><h3>aa组件</h3><button @click="sendMsg">将数据发送给bb组件</button></div>
</template>
<script>
import bus from './bus'
export default {methods: {sendMsg(){bus.$emit('sendTitle','传递的值')}     }
}
</script>
// 组件 bb,接收数据
<template><div>接收aa传递过来的值:{{msg}}</div>
</template>
<script>
import bus from './bus'
export default {data(){return {mag: ''}}mounted(){bus.$on('sendTitle',(val)=>{this.mag = val})}
}
</script>

6. $root

$root 获取当前 vue 组件的根组件实例。

当兄弟组件之间传值,或者是跨层级传值时,可以将数据挂载到根实例上,然后其他组件从根实例上获取。

// child1.vue
methods: {setData () {this.$root.myData = '使用$root传递数据'}
}// child2.vue
methods: {getData () {console.log(this.$root.myData)}
}

7. ref

如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子组件上,引用就指向组件实例。

给子组件添加 ref ,在父组件中可以通过 this.$refs.['设置的名称'] 访问子组件实例。

// parent.vue
<template><div class="border"><h2>parent</h2><child2 ref="child2"></child2><div @click="getChild">获取子节点数据</div></div>
</template><script>
import Child2 from './child2.vue'
export default {name: 'parent',components: { Child2 },methods: {getChild () {console.log(this.$refs.child2.type)}}
}
</script>

// child2.vue
<script>
export default {name: 'child2',data () {return {type: 'css'}}
}
</script>

需要注意的是,如果组件有循环,那 this.$refs.child2 获取到的是 Array:

<child2 ref="child2"v-for="(item, index) in list":key="index"></child2>export default {name: 'parent',data () {return {list: ['a', 'b', 'c']}}
}

此时 this.$refs.child2 的值为

8. vuex

组件间传值,也可以使用 vuex。利用 vuex 实现数据共享。

这篇关于vue 的组件之间传值(父子组件,兄弟组件,祖孙组件,任意组件)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/598334

相关文章

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F