Vue 组件间传值
前言
Vue 作为现在比较火的框架之一,相信您在使用的过程中,也会遇到组件间传值的情况,本文将讲解几种 Vue 组件间传值的几种方法,跟着小编一起来学习一下吧!
实现
注意: 学习本文,需要您对 Vue 有一定的了解。
为了便于讲解,以下方法均假设父组件是 FatherComponent,子组件是 ChildComponent,兄弟组件是:BrotherComponent。我们来假定一种场景:点击父组件“传递给子组件”按钮,向子组件传递一条消息“I am your father.”;点击子组件“传递给父组件”按钮,向父组件传递一条消息“I am your child.”;点击子组件“传递给兄弟组件”按钮,向兄弟组件传递一条消息“I am your brother.”
1. 方法一
关键词: props、$emit
父组件 FatherComponent 代码:
<template>
<div>
<div>{{toFatherInfo}}</div>
<ChildComponent :toChildInfo="toChildInfo" @toFather="toFather" @toBrother="toBrother"/>
<BrotherComponent :toBrotherInfo="toBrotherInfo"/>
<button @click="toChild">传递给子组件</button>
</div>
</template>
<script>
import ChildComponent from 'components/test/child-component'
import BrotherComponent from 'components/test/brother-component'
export default {
components: {
ChildComponent,
BrotherComponent
},
data () {
return {
toFatherInfo: '',
toChildInfo: '',
toBrotherInfo: ''
}
},
methods: {
toFather (res) {
this.toFatherInfo = res
},
toBrother (res) {
this.toBrotherInfo = res
},
toChild () {
this.toChildInfo = 'I am your father.'
}
}
}
</script>
<style lang="less">
button {
font-size: 36px;
border: none;
padding: 20px;
background-color: #999;
color: #fff;
width: 100%;
margin-top: 30px;
}
</style>
子组件 ChildComponent 代码:
<template>
<div>
<div>{{toChildInfo}}</div>
<button @click="toFather">传递给父组件</button>
<button @click="toBrother">传递给兄弟组件</button>
</div>
</template>
<script>
export default {
props: {
toChildInfo: {
type: String
}
},
methods: {
toFather () {
this.$emit('toFather', 'I am your child.')
},
toBrother () {
this.$emit('toBrother', 'I am your brother.')
}
}
}
</script>
<style lang="less">
</style>
兄弟组件 BrotherComponent 代码:
<template>
<div>{{toBrotherInfo}}</div>
</template>
<script>
export default {
props: {
toBrotherInfo: {
type: String
}
}
}
</script>
<style lang="less">
</style>
通过上面代码,不难发现,我们通过使用 props 来实现父组件给子组件传值;子组件向父组件传值时,借助 $emit 来实现;而子组件向兄弟组件传值时,将两者结合起来使用。
2. 方法二
关键词:独立的事件中心 eventHub
首先需要先创建 eventHub.js 文件,代码如下:
// 将在各处使用该事件中心
// 组件通过它来通信
import Vue from 'vue'
export default new Vue()
然后在组件中,可以使用 $emit, $on, $off 分别来分发、监听、取消监听事件。
父组件 FatherComponent 代码:
<template>
<div>
<div>{{info}}</div>
<ChildComponent />
<BrotherComponent />
<button @click="toChild">传递给子组件</button>
</div>
</template>
<script>
import eventHub from '../../components/test/eventHub'
import ChildComponent from 'components/test/child-component'
import BrotherComponent from 'components/test/brother-component'
export default {
components: {
ChildComponent,
BrotherComponent
},
data () {
return {
info: ''
}
},
created: function () {
eventHub.$on('toFather', this.toFather)
},
// 最好在组件销毁前
// 清除事件监听
beforeDestroy: function () {
eventHub.$off('toFather', this.toFather)
},
methods: {
toFather (res) {
this.info = res
},
toChild () {
eventHub.$emit('toChild', 'I am your father.')
}
}
}
</script>
<style lang="less">
button {
font-size: 36px;
border: none;
padding: 20px;
background-color: #999;
color: #fff;
width: 100%;
margin-top: 30px;
}
</style>
子组件 ChildComponent 代码:
<template>
<div>
<div>{{info}}</div>
<button @click="toFather">传递给父组件</button>
<button @click="toBrother">传递给兄弟组件</button>
</div>
</template>
<script>
import eventHub from './eventHub'
export default {
data () {
return {
info: ''
}
},
created: function () {
eventHub.$on('toChild', this.toChild)
},
// 最好在组件销毁前
// 清除事件监听
beforeDestroy: function () {
eventHub.$off('toChild', this.toChild)
},
methods: {
toChild (res) {
this.info = res
},
toFather () {
eventHub.$emit('toFather', 'I am your child.')
},
toBrother () {
eventHub.$emit('toBrother', 'I am your brother.')
}
}
}
</script>
<style lang="less">
</style>
兄弟组件 BrotherComponent 代码:
<template>
<div>{{info}}</div>
</template>
<script>
import eventHub from './eventHub'
export default {
data () {
return {
info: ''
}
},
created: function () {
eventHub.$on('toBrother', this.toBrother)
},
// 最好在组件销毁前
// 清除事件监听
beforeDestroy: function () {
eventHub.$off('toBrother', this.toBrother)
},
methods: {
toBrother (res) {
this.info = res
}
}
}
</script>
<style lang="less">
</style>
3. 方法三
关键词:Vuex
我们需要创建 store.js 来存放数据:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
fromFatherInfo: '',
fromChildInfo: '',
fromBrotherInfo: ''
},
mutations: {
changeFromFatherInfo (state, fromFatherInfo) {
state.fromFatherInfo = fromFatherInfo
},
changeFromChildInfo (state, fromChildInfo) {
state.fromChildInfo = fromChildInfo
},
changeFromBrotherInfo (state, fromBrotherInfo) {
state.fromBrotherInfo = fromBrotherInfo
}
}
})
实例化:
import Vue from 'vue'
import App from './App'
import store from './store'
new Vue({
el: '#app',
store,
template: '<App/>',
components: { App }
})
父组件 FatherComponent 代码:
<template>
<div>
<div>{{fromChildInfo}}</div>
<ChildComponent />
<BrotherComponent />
<button @click="toChild">传递给子组件</button>
</div>
</template>
<script>
import ChildComponent from 'components/test/child-component'
import BrotherComponent from 'components/test/brother-component'
export default {
components: {
ChildComponent,
BrotherComponent
},
computed: {
fromChildInfo () {
return this.$store.state.fromChildInfo
}
},
methods: {
toChild () {
this.$store.commit('changeFromFatherInfo', 'I am your father.')
}
}
}
</script>
<style lang="less">
button {
font-size: 36px;
border: none;
padding: 20px;
background-color: #999;
color: #fff;
width: 100%;
margin-top: 30px;
}
</style>
子组件 ChildComponent 代码:
<template>
<div>
<div>{{fromFatherInfo}}</div>
<button @click="toFather">传递给父组件</button>
<button @click="toBrother">传递给兄弟组件</button>
</div>
</template>
<script>
export default {
computed: {
fromFatherInfo () {
return this.$store.state.fromFatherInfo
}
},
methods: {
toFather () {
this.$store.commit('changeFromChildInfo', 'I am your child.')
},
toBrother () {
this.$store.commit('changeFromBrotherInfo', 'I am your brother.')
}
}
}
</script>
<style lang="less">
</style>
兄弟组件 BrotherComponent 代码:
<template>
<div>{{fromBrotherInfo}}</div>
</template>
<script>
export default {
computed: {
fromBrotherInfo () {
return this.$store.state.fromBrotherInfo
}
}
}
</script>
<style lang="less">
</style>
来源:https://segmentfault.com/a/1190000018088789
Vue 组件间传值的更多相关文章
- Vue组件间传值 v-model
使用过Vue的同学应该都了解组件之间传值 父组件 --> 子组件 : props 子组件 --> 父组件 : 事件 其实有一种更为简单的方法,是基于上述两种方法,那就是 v-model 我 ...
- vue 组件间传值方式
/* 父组件给子组件传值 1.父组件调用子组件的时候 绑定动态属性 <v-header :title="title"></v-header> 2.在子组件里 ...
- vue组件间传值详解
1.父传子----传值要点: <1> 在组件注册的时候必须要使用 return 去返回 data对象;
- vue组件间传值
父传子 1.父组件:在子组件容器内绑定数据 <router-view :unusedOrderNum="num1" :usedOrderNum="num2" ...
- Vue中组件间传值常用的几种方式
版本说明: vue-cli:3.0 一.父子组件间传值 1.props/$emit -父组件==>>子组件: 子组件中通过定义props接收父组件中通过v-bind绑定的数据 父组件代码 ...
- Vue学习(二)-Vue中组件间传值常用的几种方式
版本说明:vue-cli:3.0 主要分为两类: 1.父子组件间的传值 2.非父子组件间的传值 1.父子组件间传值 父组件向子组件传值 第一种方式: props 父组件嵌套的子组件中,使用v-bind ...
- vue 组件与传值
一.表单输入绑定(v-model 指令) 可以用 v-model 指令在表单 <input>.<textarea> 及 <select> 元素上创建双向数据绑定. ...
- vue组件间通信六种方式(完整版)
本文总结了vue组件间通信的几种方式,如props. $emit/ $on.vuex. $parent / $children. $attrs/ $listeners和provide/inject,以 ...
- Vue组件间通信-Vuex
上回说到Vue组件间通讯,最后留了一个彩蛋~~~Vuex.Vuex是另一种组件通讯的方法,这节来说说Vuex(store仓库). 首先Vuex需要安装,安装的方式有很多,在这里就不一一细说了.我是通过 ...
随机推荐
- [2011WorldFinal]Chips Challenge[流量平衡]
Chips Challenge Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- PMP 质量管理新7张图
亲和图.关联图.系统图.矩阵图.网络图.pdpc.矩阵数据分析法
- Android 获取当前应用的版本号和当前系统的版本号
1.获取当前程序版本名 我们可以在AndroidManifest.xml中设置程序的版本号等,如android:versionName="1.0",那如果想在代码中获取这个版本号呢 ...
- Exchange NLB 单播和多播模式比较
一般来说,在NLB的创建时,单网卡多播,双网卡单播. 双网卡单播时,因为主机之间不能互相通信,将设置内网通讯的网卡,也就是群集设置中的心跳. NLB模式 描述 优点 缺点 单播 网卡MAC会被NLB专 ...
- npm install命令对package-lock.json文件自动做了一些额外的更新
今天我使用 npm 命令给项目安装file-saver,通过git却发现package-lock.json中除了file-saver组件之外的其他组件的记录也被改了 npm为何会自动做这些更改呢,又如 ...
- Proud Merchants---hdu3466(有01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3466 与顺序有关的01背包. 如果一个物品p = 5,q = 7,一个物品p = 5,q = 9,如果 ...
- django组件之ContentType
ContentTyep组件: 帮助我们关联所有数据库的表 帮助我们反向查询关联数据表中的所有策略信息 GenericForeignkey(帮助我们快速插入数据) GenericRelation(用于反 ...
- Spark Streaming实战
1.Storm 和 SparkStreaming区别 Storm 纯实时的流式处理,来一条数据就立即进行处理 SparkStreaming 微批处理,每次处理 ...
- lnmp1.4环境下thinkphp3.2配置pathinfo模式
1.打开php.ini 通常该文件在 /usr/local/php/etc/php.ini vi /usr/local/php/etc/php.ini 找到 cgi.fix_pathinfo,默认为0 ...
- oracle的字符集设置与乱码
oracle的字符集设置与乱码 字符集问题一直叫人头疼,究其原因还是不能完全明白其运作原理. 在整个运行环节中,字符集在3个环节中发挥作用: 1.软件在操作系统上运作时的对用户的显示,此时采用操作系统 ...