Vue之组件间传值
标签: Vue
Vue之父子组件传值
- 父向子传递通过props
- 子向父传递通过$emit
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>父子组件传值</title>
<script src="./bower_components/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<counter :count="num1" @add="handAddTotal"></counter>
<counter :count="num2" @add="handAddTotal"></counter>
求和:{{total}}
</div>
<script>
//自定义组件
var counter = {
props:['count'],//通过属性由父向子传值
data: function() {
return {
number: this.count//子组件内接收父级传过来的值
}
},
template: '<div @click="handleClick">{{number}}</div>',
methods: {
handleClick: function() {
this.number ++;
//通过向外触发事件由子级向父级传值
this.$emit('add',1);
}
}
};
var vm = new Vue({
el: '#app',
//组件注册
components: {
counter
},
data:{
num1:1,
num2:2,
total: 3
},
methods:{
//求和
handAddTotal:function(step){
this.total += step;
}
}
});
</script>
</body>
</html>
注意事项:
- props传过来值,根据单向数据流原则子组件不可直接拿过来修改,可以在子组件的data里定义一个变量转存再来做修改
- 为了保证各组件数据的独立性,子组件的data应该以一个函数返回一个对象的形式来定义,见上示例代码23行
Vue之非父子组件传值
- 通过bus方式传递,也可以叫总线/发布订阅模式/观察者模式
- 通过vuex传递
bus/总线/发布订阅模式/观察者模式演示地址
vuex演示地址
bus方式示例代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>父子组件传值(bus/总线/发布订阅模式/观察者模式)</title>
<script src="./bower_components/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<child-one content="hello"></child-one>
<child-two content="world"></child-two>
</div>
<script>
Vue.prototype.bus = new Vue();
//自定义组件
var childOne = {
//通过属性由父向子传值
props:{
'content':String
},
data:function(){
return {
contentIn:this.content
}
},
template: '<div @click="handleClick">{{contentIn}}</div>',
methods: {
handleClick: function() {
//通过触发事件向各组件发送数据
this.bus.$emit('change',this.contentIn);
}
},
mounted:function () {
var that = this;
//组件接收事件
this.bus.$on('change',function(val){
that.contentIn = val;
});
}
};
//自定义组件
var childTwo = {
//通过属性由父向子传值
props:{
'content':String
},
data:function(){
return {
contentIn:this.content
}
},
template: '<div @click="handleClick">{{contentIn}}</div>',
methods: {
handleClick: function() {
//通过触发事件向各组件发送数据
this.bus.$emit('change',this.contentIn);
}
},
mounted:function () {
var that = this;
//组件接收事件
this.bus.$on('change',function(val){
that.contentIn = val;
});
}
};
var vm = new Vue({
el: '#app',
//注册组件
components: {
childOne,
childTwo
}
});
</script>
</body>
</html>
vuex方式示例代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>父子组件传值(vuex)</title>
<script src="./bower_components/vue/dist/vue.js"></script>
<script src="./bower_components/vuex/dist/vuex.js"></script>
</head>
<body>
<div id="app">
<child-one></child-one>
<child-two></child-two>
</div>
<script>
Vue.use(Vuex);
var store = new Vuex.Store({
state: {
count:0
},
mutations: {
increment:function(state){
console.log(123);
state.count++;
}
},
actions: {
increment:function(context){
context.commit('increment')
}
},
getters: {
getCount:function(state){
return state.count;
}
}
});
//自定义组件
var childOne = {
computed: {
countIn:function(){
return store.getters.getCount
}
},
template: '<div @click="handleClick">{{countIn}}</div>',
methods: {
handleClick: function() {
store.dispatch('increment');
}
}
};
//自定义组件
var childTwo = {
computed: {
countIn:function(){
return store.getters.getCount
}
},
template: '<div @click="handleClick">{{countIn}}</div>',
methods: {
handleClick: function() {
store.dispatch('increment');
}
}
};
var vm = new Vue({
el: '#app',
store,
//注册组件
components: {
childOne,
childTwo
}
});
</script>
</body>
</html>
附上vuex官网地址:https://vuex.vuejs.org/zh/
Vue之组件间传值的更多相关文章
- Vue中组件间传值常用的几种方式
版本说明: vue-cli:3.0 一.父子组件间传值 1.props/$emit -父组件==>>子组件: 子组件中通过定义props接收父组件中通过v-bind绑定的数据 父组件代码 ...
- Vue学习(二)-Vue中组件间传值常用的几种方式
版本说明:vue-cli:3.0 主要分为两类: 1.父子组件间的传值 2.非父子组件间的传值 1.父子组件间传值 父组件向子组件传值 第一种方式: props 父组件嵌套的子组件中,使用v-bind ...
- vue——父子组件间传值
(1)父组件给子组件传值(商品详情页): 根据订单类型,判断显示立即购买/立即拼单: 通过props来传递参数 父组件(商品详情页) 父组件调用子组件,在子组件的标签中,通过:数据名称=”数据”的形式 ...
- Vue 组件间传值
前言 Vue 作为现在比较火的框架之一,相信您在使用的过程中,也会遇到组件间传值的情况,本文将讲解几种 Vue 组件间传值的几种方法,跟着小编一起来学习一下吧! 实现 注意: 学习本文,需要您对 Vu ...
- vue组件定义方式,vue父子组件间的传值
vue组件定义方式,vue父子组件间的传值 <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...
- Vue中组件间通信的方式
Vue中组件间通信的方式 Vue中组件间通信包括父子组件.兄弟组件.隔代组件之间通信. props $emit 这种组件通信的方式是我们运用的非常多的一种,props以单向数据流的形式可以很好的完成父 ...
- vue父子组件之间传值
vue父子组件进行传值 vue中的父子组件,什么是父组件什么是子组件呢?就跟html标签一样,谁包裹着谁谁就是父组件,被包裹的元素就是子组件. 父组件向子组件传值 下面用的script引入的方式,那种 ...
- vue父子组件的传值总结
久违的博客园我又回来了.此篇文章写得是vue父子组件的传值,虽然网上已经有很多了.写此文章的目的就是记录下个人学习的一部分.接下来我们就进入主题吧! 在开发vue项目中,父子组件的传值是避免不掉的. ...
- react组件间传值详解
一.父子组件间传值 <1>父传子 父组件:
随机推荐
- hdu4651(广义五边形数 & 分割函数1)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4651 题意:f(x) 为将 x 分成其他数和的形式的方案数.对于 t 组输入,输出 f(xi). 思路 ...
- 智能合约安全事故回顾(2)-BEC溢出攻击
讲溢出攻击之前,先给大家讲个故事:2014年的时候,美国的宾夕法尼亚州的某个小镇上发生了一个乌龙事件,征兵系统对一万多名1893年到1897出生的男子发去信函,要求他们注册参军,否则面临罚款和监禁.收 ...
- Spring IOC机制使用SpEL
一.SpEL 1.1 简介 Spring Expression Language,Spring表达式语言,简称SpEL.支持运行时查询并可以操作对象图. 和JSP页面上的EL表达式.Str ...
- (Python OpenGL)【 0】关于VAO和VBO以及OpenGL新特性
(Python OpenGL)关于新版OpenGL需要了解的: 随着OpenGL状态和固定管线模式的移除,我们不在用任何glEnable函数调用,而且也不会有glVertex.glColor等函数调用 ...
- P3241 [HNOI2015]开店 动态点分治
\(\color{#0066ff}{ 题目描述 }\) 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱. 这样的想 ...
- [USACO5.4]奶牛的电信Telecowmunication 最小割
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
- CF959D Mahmoud and Ehab and another array construction task 数学
Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same l ...
- kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Windows自动化---模拟鼠标键盘
1.PyUserInput(不推荐) python2可以使用PyUserInput库:(不推荐) 支持最基础的鼠标,键盘操作,可以剪贴. 安装的时候:pip install PyUserInput 需 ...
- 封装下Excel导出
1. 使用方法 1.1 对象使用注解 @ExcelColumn(name = "页面1",freeze = "0,1,1,2",autoWidth=true) ...