浅谈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 相关文章推荐
同时使用n个window onload加载实例介绍
Apr 25 Javascript
动态加载iframe时get请求传递中文参数乱码解决方法
May 07 Javascript
在JS数组特定索引处指定位置插入元素的技巧
Aug 24 Javascript
javascript动态生成树形菜单的方法
Nov 14 Javascript
原生 JS Ajax,GET和POST 请求实例代码
Jun 08 Javascript
原生js实现addclass,removeclass,toggleclasss实例
Nov 24 Javascript
微信小程序教程系列之视图层的条件渲染(10)
Apr 19 Javascript
JS中的数组转变成JSON格式字符串的方法
May 09 Javascript
详细介绍RxJS在Angular中的应用
Sep 23 Javascript
arcgis for js栅格图层叠加(Raster Layer)问题
Nov 22 Javascript
微信小程序实现的日期午别医生排班表功能示例
Jan 09 Javascript
微信小程序如何使用canvas二维码保存至手机相册
Jul 15 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 删除cookie和浏览器重定向
2009/03/16 PHP
php使用Smarty的相关注意事项及访问变量的几种方式
2011/12/08 PHP
PHP中设置时区方法小结
2012/06/03 PHP
PHP序列化操作方法分析
2016/09/28 PHP
YII框架行为behaviors用法示例
2019/04/26 PHP
自己动手开发jQuery插件教程
2011/08/25 Javascript
修改js Calendar日历控件 兼容IE9/谷歌/火狐
2013/01/04 Javascript
javascript中比较字符串是否相等的方法
2013/07/23 Javascript
利用JQuery和Servlet实现跨域提交请求示例分享
2014/02/12 Javascript
javascript学习笔记(三)BOM和DOM详解
2014/09/30 Javascript
jQuery实现鼠标划过修改样式的方法
2015/04/14 Javascript
jQuery选择器用法实例详解
2015/12/17 Javascript
项目实践一图片上传之form表单还是base64前端图片压缩(前端图片压缩)
2016/07/28 Javascript
JavaScript中 DOM操作方法小结
2017/04/25 Javascript
Vue实现用户自定义字段显示数据的方法
2018/08/28 Javascript
vue 点击按钮增加一行的方法
2018/09/07 Javascript
Python+Django在windows下的开发环境配置图解
2009/11/11 Python
Python使用matplotlib实现在坐标系中画一个矩形的方法
2015/05/20 Python
Python实现定时任务
2017/02/08 Python
python虚拟环境的安装配置图文教程
2017/10/20 Python
python中abs&amp;map&amp;reduce简介
2018/02/20 Python
基于numpy中数组元素的切片复制方法
2018/11/15 Python
Python 加密与解密小结
2018/12/06 Python
python标记语句块使用方法总结
2019/08/05 Python
解决json中ensure_ascii=False的问题
2020/04/03 Python
Python连接Hadoop数据中遇到的各种坑(汇总)
2020/04/14 Python
文员岗位职责范本
2014/03/08 职场文书
绩效管理实施方案
2014/03/19 职场文书
《鲁班和橹板》教学反思
2014/04/27 职场文书
群众路线教育实践活动整改落实情况汇报
2014/10/28 职场文书
先进教师个人事迹材料
2014/12/15 职场文书
本溪水洞导游词
2015/02/11 职场文书
警告通知
2015/04/25 职场文书
2016圣诞节贺卡寄语
2015/12/07 职场文书
Spring mvc是如何实现与数据库的前后端的连接操作的?
2021/06/30 Java/Android
Android开发手册Chip监听及ChipGroup监听
2022/06/10 Java/Android