浅谈vue的props,data,computed变化对组件更新的影响


Posted in Javascript onJanuary 16, 2018

本文介绍了vue的props,data,computed变化对组件更新的影响,分享给大家,废话不多说,直接上代码

/** this is Parent.vue */
<template>
 <div>
  <div>{{'parent data : ' + parentData}}</div>
  <div>{{'parent to children1 props : ' + parentToChildren1Props}}</div>
  <div>{{'parent to children2 props : ' + parentToChildren2Props}}</div>
  <div>
   <el-button @click="changeParentData">change parent data</el-button>
   <el-button @click="changeParentToChildren1Props">change parent to children1 data</el-button>
   <el-button @click="changeParentToChildren2Props">change parent to children2 data</el-button>
  </div>
  <my-children1 :children1Props="parentToChildren1Props" @changeParentToChildren1Props="changeParentToChildren1Props"></my-children1>
  <my-children2 :children2Props="parentToChildren2Props" @changeParentToChildren2Props="changeParentToChildren2Props"></my-children2>
 </div>
</template>

<script>
 import Children1 from './Children1';
 import Children2 from './Children2';
 export default{
  name: 'parent',
  data() {
   return {
    parentData: 'ParentData',
    parentToChildren1Props: 'ParentToChildren1Props',
    parentToChildren2Props: 'ParentToChildren2Props'
   }

  },

  beforeCreate: function() {
   console.log('*******this is parent beforeCreate*********');

  },

  created: function() {
   console.log('######this is parent created######');

  },

  beforeMount: function() {
   console.log('------this is parent beforeMount------');

  },

  mounted: function() {
   console.log('++++++this is parent mounted++++++++');

  },

  beforeUpdate: function() {
   console.log('&&&&&&&&this is parent beforeUpdate&&&&&&&&');

  },

  updated: function() {
   console.log('$$$$$$$this is parent updated$$$$$$$$');

  },

  methods: {
   changeParentData: function() {
    this.parentData = 'changeParentData'

   },

   changeParentToChildren1Props: function() {
    this.parentToChildren1Props = 'changeParentToChildren1Props'

   },

   changeParentToChildren2Props: function() {
    this.parentToChildren2Props = 'changeParentToChildren2Props'

   }

  },
  components: {
   'my-children1': Children1,
   'my-children2': Children2
  }
 }
</script>
/** this is Children1.vue */
<template>
 <div>
  <div>{{'children1 data : ' + children1Data}}</div>
  <div>{{'parent to children1 props : ' + children1Props}}</div>
  <div>{{'parent to children1 props to data : ' + children1PropsData}}</div>
  <div>
   <el-button @click.native="changeChildren1Data">change children1 data</el-button>
   <el-button @click.native="emitParentToChangeChildren1Props">emit parent to change children1 props</el-button>
  </div>
 </div>
</template>

<script>
 export default {
  name: 'children1',
  props: ['children1Props'],
  data() {
   return {
    children1Data: 'Children1Data'
   }
  },

  computed: {
   children1PropsData: function() {
    return this.children1Props
   }
  },

  beforeCreate: function() {
   console.log('*******this is children1 beforeCreate*********');

  },

  created: function() {

   console.log('######this is children1 created######');
  },

  beforeMount: function() {
   console.log('------this is children1 beforeMount------');

  },

  mounted: function() {
   console.log('++++++this is children1 mounted++++++++');

  },

  beforeUpdate: function() {
   console.log('&&&&&&&&this is children1 beforeUpdate&&&&&&&&');

  },

  updated: function() {
   console.log('$$$$$$$this is children1 updated$$$$$$$$');

  },

  methods: {
   changeChildren1Data: function() {
    this.children1Data = 'changeChildren1Data'

   },

   emitParentToChangeChildren1Props: function() {
    console.log('emitParentToChangeChildren1Props');
    this.$emit('changeParentToChildren1Props');
   }
  }
 }
</script>
/** this is Children2.vue */
<template>
 <div>
  <div>{{'children2 data : ' + children2Data}}</div>
  <div>{{'parent to children2 props : ' + children2Props}}</div>
  <div>{{'parent to children2 props to data : ' + children2PropsData}}</div>
  <div>
   <el-button @click.native="changeChildren2Data">change children2 data</el-button>
   <el-button @click.native="emitParentToChangeChildren2Props">emit parent to change children2 props</el-button>
  </div>
 </div>
</template>

<script>
 export default {
  name: 'children2',
  props: ['children2Props'],
  data() {
   return {
    children2Data: 'Children2Data',
    children2PropsData: this.children2Props
   }
  },

  beforeCreate: function() {
   console.log('*******this is children2 beforeCreate*********');

  },

  created: function() {
   console.log('######this is children2 created######');

  },

  beforeMount: function() {
   console.log('------this is children2 beforeMount------');

  },

  mounted: function() {
   console.log('++++++this is children2 mounted++++++++');

  },

  beforeUpdate: function() {
   console.log('&&&&&&&&this is children2 beforeUpdate&&&&&&&&');

  },
  updated: function() {
   console.log('$$$$$$$this is children2 updated$$$$$$$$');

  },

  methods: {
   changeChildren2Data: function() {
    this.children2Data = 'changeChildren2Data'
   },

   emitParentToChangeChildren2Props: function() {
    this.$emit('changeParentToChildren2Props');
   }
  }
 }
</script>
  1. 父组件改变props,子组件如果直接使用props,会触发子组件更新
  2. 父组件改变props,子组件如果将props放进data中再使用,不会触发子组件更新
  3. 父组件改变props,子组件如果将props放进computed中再使用,会触发子组件更新
  4. data,props和computed的变化都会触发组件更新

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript showModalDialog 多层模态窗口实现页面提交及刷新的代码
Nov 28 Javascript
JavaScript中String和StringBuffer的速度之争
Apr 01 Javascript
JavaScript中将一个值转换为字符串的方法分析[译]
Sep 21 Javascript
Js操作树节点自动折叠展开的几种方法
May 05 Javascript
JavaScript控制table某列不显示的方法
Mar 16 Javascript
JavaScript实现多个重叠层点击切换效果的方法
Apr 24 Javascript
js实现键盘上下左右键选择文字并显示在文本框的方法
May 07 Javascript
Angular.js中angular-ui-router的简单实践
Jul 18 Javascript
解决Js先触发失去焦点事件再执行点击事件的问题
Aug 30 Javascript
在移动端使用vue-router和keep-alive的方法示例
Dec 02 Javascript
Vue和React组件之间的传值方式详解
Jan 31 Javascript
浅谈Vuex的this.$store.commit和在Vue项目中引用公共方法
Jul 24 Javascript
Parcel 打包示例(React HelloWorld)
Jan 16 #Javascript
详解Vue快速零配置的打包工具——parcel
Jan 16 #Javascript
vue watch自动检测数据变化实时渲染的方法
Jan 16 #Javascript
动态加载权限管理模块中的Vue组件
Jan 16 #Javascript
vue2.0 兄弟组件(平级)通讯的实现代码
Jan 15 #Javascript
解析Angular 2+ 样式绑定方式
Jan 15 #Javascript
基于jquery的on和click的区别详解
Jan 15 #jQuery
You might like
PHP Google的translate API代码
2008/12/10 PHP
php 上传功能实例代码
2010/04/13 PHP
php生成随机密码的三种方法小结
2010/09/04 PHP
使用JSON实现数据的跨域传输的php代码
2011/12/20 PHP
PHP实现双链表删除与插入节点的方法示例
2017/11/11 PHP
Ubuntu 11.10 安装Node.js的方法
2011/11/30 Javascript
javascript中判断一个值是否在数组中并没有直接使用
2012/12/17 Javascript
JavaScript对象和字串之间的转换实例探讨
2013/04/21 Javascript
浅谈Jquery核心函数
2015/06/18 Javascript
详解javascript遍历方式
2015/11/11 Javascript
switch语句的妙用(必看篇)
2016/10/03 Javascript
把json格式的字符串转换成javascript对象或数组的方法总结
2016/11/03 Javascript
js 能实现监听F5页面刷新子iframe 而父页面不刷新的方法
2016/11/09 Javascript
详解利用 Vue.js 实现前后端分离的RBAC角色权限管理
2017/09/15 Javascript
ECMAScript6变量的解构赋值实例详解
2017/09/19 Javascript
vue项目中应用ueditor自定义上传按钮功能
2018/04/27 Javascript
微信小程序实现红包功能(后端PHP实现逻辑)
2018/07/11 Javascript
JavaScript设计模式之职责链模式应用示例
2018/08/07 Javascript
微信小程序批量上传图片到七牛(推荐)
2019/12/19 Javascript
js实现选项卡效果
2020/03/07 Javascript
Python计算一个文件里字数的方法
2015/06/15 Python
一道python走迷宫算法题
2018/01/22 Python
python列表每个元素同增同减和列表元素去空格的实例
2019/07/20 Python
Pyqt5自适应布局实例
2019/12/13 Python
利用PyTorch实现VGG16教程
2020/06/24 Python
html5拖拽应用记录及注意点
2020/05/27 HTML / CSS
荷兰游戏商店:Allyouplay
2019/03/16 全球购物
会计专业毕业生自荐信范文
2013/12/20 职场文书
体育教师自我鉴定
2014/02/12 职场文书
喷漆工的岗位职责
2014/03/17 职场文书
歌剧魅影观后感
2015/06/05 职场文书
2015年国庆节寄语
2015/08/17 职场文书
《金色的草地》教学反思
2016/02/17 职场文书
2019年员工旷工保证书!
2019/06/28 职场文书
nginx+lua单机上万并发的实现
2021/05/31 Servers
php修改word的实例方法
2021/11/17 PHP