vue2.0组件传值
props down emit up
嘿嘿 如果是第一次接触vue2.0组件传值的肯定很疑惑,这是什么意思(大神总结的,我也就是拿来用用)
“down”—>指的是下的意思,即父组件向子组件传值,用props;“up”—>指的是上的意思,即子组件想父组件传值,用emit。
1.子组件向父组件的传值:
Child.vue
<template>
<div class="child">
<h1>子组件</h1>
<button v-on:click="childToParent">想父组件传值</button>
</div>
</template>
<script>
export default{
name: 'child',
data(){
return {}
},
methods: {
childToParent(){
this.$emit("childToParentMsg", "子组件向父组件传值");
}
}
}
</script> parent.vue
<template>
<div class="parent">
<h1>父组件</h1>
<Child v-on:childToParentMsg="showChildToParentMsg" ></Child>
</div>
</template>
<script>
import Child from './child/Child.vue'
export default{
name:"parent",
data(){
return {
}
},
methods: {
showChildToParentMsg:function(data){
alert("父组件显示信息:"+data)
}
},
components: {Child}
}
</script>
2.父组件向子组件传值
parent.vue
<template>
<div class="parent">
<h1>父组件</h1>
<Child v-bind:parentToChild="parentMsg"></Child>
</div>
</template>
<script>
import Child from './child/Child.vue'
export default{
name:"parent",
data(){
return {
parentMsg:'父组件向子组件传值'
}
},
components: {Child}
}
</script>
child.vue
<template>
<div class="child">
<h1>子组件</h1>
<span>子组件显示信息:{{parentToChild}}</span><br>
</div>
</template>
<script>
export default{
name: 'child',
data(){
return {}
},
props:["parentToChild"]
}
</script>
3.采用eventBus.js传值---兄弟组件间的传值
eventBus.js
import Vue from 'Vue'
export default new Vue()
App.vue
<template>
<div id="app">
<secondChild></secondChild>
<firstChild></firstChild>
</div>
</template> <script>
import FirstChild from './components/FirstChild'
import SecondChild from './components/SecondChild' export default {
name: 'app',
components: {
FirstChild,
SecondChild,
}
}
</script>
FirstChild.vue
<template>
<div class="firstChild">
<input type="text" placeholder="请输入文字" v-model="message">
<button @click="showMessage">向组件传值</button>
<br>
<span>{{message}}</span>
</div>
</template>
<script>
import bus from '../assets/eventBus';
export default{
name: 'firstChild',
data () {
return {
message: '你好'
}
},
methods: {
showMessage () {
alert(this.message)
bus.$emit('userDefinedEvent', this.message);//传值
}
}
}
</script>
SecondChild.vue
<template>
<div id="SecondChild">
<h1>{{message}}</h1>
</div>
</template>
<script>
import bus from '../assets/eventBus';
export default{
name:'SecondChild',
data(){
return {
message: ''
}
},
mounted(){
var self = this;
bus.$on('userDefinedEvent',function(message){
self.message = message;//接值
});
}
}
vue2.0组件传值的更多相关文章
- 通信vue2.0组件
vue2.0组件通信各种情况总结与实例分析 Props在vue组件中各种角色总结 在Vue中组件是实现模块化开发的主要内容,而组件的通信更是vue数据驱动的灵魂,现就四种主要情况总结如下: 使用p ...
- vue2.0组件库
UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...
- vue2.0 组件化及组件传值
组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能.在有些情况下, ...
- vue2.0组件之间的传值
1.父子组件--props props:需要注意的是,虽然的是单向的数据流,但是如果传递的是数组或是对象这样的引用类型,子组件数据变化,父组件的数据通也会变化 子组件代码 <template&g ...
- Vue2.0组件间数据传递
Vue1.0组件间传递 使用$on()监听事件: 使用$emit()在它上面触发事件: 使用$dispatch()派发事件,事件沿着父链冒泡: 使用$broadcast()广播事件,事件向下传导给所有 ...
- Vue2.0组件之间通信(转载)
Vue中组件这个特性让不少前端er非常喜欢,我自己也是其中之一,它让前端的组件式开发更加合理和简单.笔者之前有写过一篇Vue2.0子父组件通信,这次我们就来聊一聊平级组件之间的通信. 首先我们先搭好开 ...
- Vue2.0组件之间通信
Vue中组件这个特性让不少前端er非常喜欢,我自己也是其中之一,它让前端的组件式开发更加合理和简单.笔者之前有写过一篇Vue2.0子父组件通信,这次我们就来聊一聊平级组件之间的通信. 首先我们先搭好开 ...
- Vue2.0+组件库总结
转自:https://blog.csdn.net/lishanleilixin/article/details/84025459 UI组件 element - 饿了么出品的Vue2的web UI工具套 ...
- 转:Vue2.0+组件库总结
UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...
随机推荐
- 【搬运】C指针 一
本文搬运自https://fishc.com.cn/forum.php?mod=viewthread&tid=71654&extra=page%3D1%26filter%3Dtypei ...
- intellij idea 2018
intellij idea 输入System.out.println()的快捷方法是:输入sout然后按tab键. 由于项目中引入了spring-boot-starter-test的依赖,也就是集成了 ...
- 安卓中location.href或者location.reload 不起作用
链接:https://www.cnblogs.com/joshua317/p/6163471.html 在移动wap中,经常会使用window.location.href去跳转页面,这个方法在绝大多数 ...
- k8s系列~mgr的应用
一 简介:今天咱们大体介绍下 这两者是如何联系的二 概念解析 pod:说下我的理解 1 pod通过yaml文件来封装docker本身+启动形式 2 pod可以运行多个docke ...
- java中String类型
string对象常用方法 string对象比较方法: string类获取包含子串的方法: 字符串和数字的转换: String类 String对象是不可改变的,字符串一旦创建,内容不能再改变. 构造字符 ...
- npm 无法安装 ionic 解决办法
一般从 node.js官网下载安装完之后,npm也会同时安装完. 如果通过 $ npm install -g cordova ionic 去安装,往往会失败.这个是由于GFW,很多插件下载不下来,还好 ...
- 【转】linux的特殊符号与正则表达式
[转]linux的特殊符号与正则表达式 第1章 linux的特殊符号 1.1 通配符 * {} 1.1.1 含义 方便查找文件 通配符是用来找文件名字的. 1.1.2 * 通过find 命令找以 . ...
- eclipse 反编译
Eclipse Class Decompiler安装此插件,可以编译源代码且调试
- Android动态控制状态栏显示和隐藏
记得之前有朋友在留言里让我写一篇关于沉浸式状态栏的文章,正巧我确实有这个打算,那么本篇就给大家带来一次沉浸式状态栏的微技巧讲解. 其实说到沉浸式状态栏这个名字我也是感到很无奈,真不知道这种叫法是谁先发 ...
- gulp-px2rem-plugin 插件的一个小bug
最近在使用这个插件的过程中发现一个bug: 不支持 含有小数的形式. 查看源码后,修改了下其中的正则,使其支持小数形式(66.66px..6px ). 作者的源码最近一次更新都在两年前,所以就简单的记 ...