vuejs组件交互 - 01 - 父子组件之间的数据交互
父子组件之间的数据交互遵循:
- props down - 子组件通过props接受父组件的数据
- events up - 父组件监听子组件$emit的事件来操作数据
示例
子组件的点击事件函数中$emit自定义事件
export default {
name: 'comment',
props: ['issue','index'],
data () {
return {
comment: '',
}
},
components: {},
methods: {
removeComment: function(index,cindex) {
this.$emit('removeComment', {index:index, cindex:cindex});
},
saveComment: function(index) {
this.$emit('saveComment', {index: index, comment: this.comment});
this.comment="";
}
},
//hook
created: function () {
//get init data
}
}
父组件监听事件
<comment v-show="issue.show_comments" :issue="issue" :index="index" @removeComment="removeComment" @saveComment="saveComment"></comment>
父组件的methods中定义了事件处理程序
removeComment: function(data) {
var index = data.index, cindex = data.cindex;
var issue = this.issue_list[index];
var comment = issue.comments[cindex];
axios.get('comment/delete/cid/'+comment.cid)
.then(function (resp) {
issue.comments.splice(cindex,1);
});
},
saveComment: function(data) {
var index = data.index;
var comment = data.comment;
var that = this;
var issue =that.issue_list[index];
var data = {
iid: issue.issue_id,
content: comment
};
axios.post('comment/save/',data)
.then(function (resp) {
issue.comments=issue.comments||[];
issue.comments.push({
cid: resp.data,
content: comment
});
});
//clear comment input
this.comment="";
}
},
注意多参数的传递是一个对象
其实还有更多的场景需要组件间通信
官方推荐的通信方式
- 首选使用Vuex
- 使用事件总线:eventBus,允许组件自由交流
- 具体可见:https://cn.vuejs.org/v2/guide/migration.html#dispatch-和-broadcast-替换
vuejs组件交互 - 01 - 父子组件之间的数据交互的更多相关文章
- ThreadLocal父子线程之间的数据传递问题
一.问题的提出 在系统开发过程中常使用ThreadLocal进行传递日志的RequestId,由此来获取整条请求链路.然而当线程中开启了其他的线程,此时ThreadLocal里面的数据将会出现无法获取 ...
- Vue2.0父子组件之间和兄弟组件之间的数据交互
熟悉了Vue.js的同级组件之间通信,写此文章,以便记录. Vue是一个轻量级的渐进式框架,对于它的一些特性和优点,请在官网上进行查看,不再赘述. 使用NPM及相关命令行工具初始化的Vue工程,目录结 ...
- vue2.0父子组件以及非父子组件如何通信
1.父组件传递数据给子组件 父组件数据如何传递给子组件呢?可以通过props属性来实现 父组件: <parent> <child :child-msg="msg" ...
- vue父子组件及非父子组件通信
1.父组件传递数据给子组件 父组件数据如何传递给子组件呢?可以通过props属性来实现 父组件: <parent> <child :child-msg="msg" ...
- vue+elementUI项目,父组件向子组件传值,子组件向父组件传值,父子组件互相传值。
vue+elementUI项目,父组件向子组件传值,子组件向父组件传值,父子组件互相传值. vue 父组件与子组件相互通信 一.父组件给子组件传值 props 实现父组件向子组件传值. 1父组件里: ...
- vue2.0父子组件以及非父子组件通信传参详解
1.父组件传递数据给子组件 父组件数据如何传递给子组件呢?可以通过props属性来实现 父组件: <parent> <child :child-msg="msg" ...
- Vue父子组件及非父子组件如何通信
1.父组件传递数据给子组件 父组件数据如何传递给子组件呢?可以通过props属性来实现 父组件: 子组件通过props来接收数据: 方式1: 方式2 : 方式3: 这样呢,就实现了父组件向子组件传递数 ...
- 简述在Vue脚手架中,组件以及父子组件(非父子组件)之间的传值
1.组件的定义 组成: template:包裹HTML模板片段(反映了数据与最终呈现给用户视图之间的映射关系) 只支持单个template标签: 支持lang配置多种模板语法: script:配置Vu ...
- vue2.0父子组件以及非父子组件通信
官网API: https://cn.vuejs.org/v2/guide/components.html#Prop 一.父子组件通信 1.父组件传递数据给子组件,使用props属性来实现 传递普通字符 ...
随机推荐
- 安全:CSRF
原文地址:http://baike.baidu.com/view/1609487.htm?fr=aladdin. 攻击通过在授权用户访问的页面中包含链接或者脚本的方式工作.例如:一个网站用户Bob可能 ...
- Ioc模式和MEF
IOC模式 Ioc模式(又称DI:Dependency Injection 依赖注射). 分离关注( Separation of Concerns : SOC)是Ioc模式和AOP产生最原始动力,通过 ...
- Logcat多tag过滤
当Android设备通过usb连接成功后,在logcat中能看到很多log信息,但太多了很容易将我们关注的日志给淹没掉,所以我们需要过滤.如果接入了不同的SDK,那么log的tag可能会不同,所以有时 ...
- Android 代码实现应用强制装到手机内存
在Froyo(android 2.2,API Level:8)中引入了android:installLocation.通过设置该属性可以使得开发者以及用户决定程序的安装位置. android:inst ...
- [Android Pro] AndroidStudio IDE界面插件开发(进阶篇之Action机制)
转载请注明出处:[huachao1001的专栏:http://blog.csdn.net/huachao1001/article/details/53883500] 从上一篇<AndroidSt ...
- 在Redhat 7.3中采用离线方式安装Docker
本文环境 Redhat Linux 7.3.Docker 18. 写在前面 Docker CE默认是不支持Redhat的,如果你想在Redhat安装,可以使用静态二进制包.这是我多次尝试RPM后得出的 ...
- emouse思·睿—评论与观点整理之四
虽说我主要做的硬件,平时的兴趣爱好比较关注移动互联网,混迹于虎嗅.爱范儿.雷锋网.36Kr.cnBeta.瘾科技.i黑马.TechWeb等这类科技以及创业媒体,遗憾的是系统的去写的并不多,好在还算充分 ...
- ubuntu 安装 android studio
总共分三个步骤: 安装JDK 这里引用下别人的安装过程http://blog.csdn.net/bhq2010/article/details/6839762 注:android studio 版本为 ...
- SharePoint 2013 开启访问请求 链接丢失
关于SharePoint 2013 开启访问请求的做法其实很简单,比如http://www.cnblogs.com/jianyus/archive/2014/06/21/3799386.html 这篇 ...
- [leetcode]Word Break @ Python
原题地址:https://oj.leetcode.com/problems/word-break/ 题意: Given a string s and a dictionary of words dic ...