标签: 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/

最新文章

  1. 类型转换bin()、chr()、ord() 、int()、float()、str()、repr()、bytes()、tuple(s&#160;)、 list(s&#160;)&#160;&#160;&#160;、unichr(x&#160;)&#160;、 ord(x&#160;)&#160;&#160;、 hex(x&#160;)&#160;&#160;、&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; type()数据类型查询
  2. 免费SVN空间
  3. MongoDB使用操作
  4. NHibernate系列文章十四:NHibernate事务
  5. C++ 构造函数中调用虚函数
  6. MVC学习IIS的不同版本(一)
  7. DNA电荷转移:电阻的计算公式 &amp; Marcus电子转移理论
  8. jetty-distribution-7.6.x 部署
  9. android 19 activity纵横屏切换的数据保存与恢复
  10. 单点登录CAS使用记(六):单点登出、单点注销
  11. Best Cow Line (POJ 3217)
  12. tomcat发布项目绑定域名总结
  13. .NET Core实战项目之CMS 第十七章 CMS网站系统的部署
  14. shiro教程1(HelloWorld)
  15. Android中加解密算法大全
  16. iOS Xcode 用 GitHub 托管项目
  17. TCHAR函数查询
  18. 葡萄城报表模板库再次更新!补充医院Dashboard及房地产销售行业报表
  19. 【虚拟化系列】VMware vSphere 5.1 网络管理
  20. shell编程小结

热门文章

  1. GuavaCache简介(一)
  2. P2891 [USACO07OPEN]吃饭Dining 最大流
  3. 关于如何使用代码触发 UIButton的Unwind Segue
  4. 论文阅读笔记五十六:(ExtremeNet)Bottom-up Object Detection by Grouping Extreme and Center Points(CVPR2019)
  5. README.md使用
  6. 解决maven项目中有小红叉的问题
  7. pre 标签 防止 其撑开 div...
  8. django 异常问题总结
  9. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_运行时解析类型引用
  10. 函数之-------------------------HR管理操作