浅谈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 相关文章推荐
为指定元素增加样式的js代码
Dec 09 Javascript
Extjs4 消息框去掉关闭按钮(类似Ext.Msg.alert)
Apr 02 Javascript
js函数在frame中的相互调用详解
Mar 03 Javascript
javascript实现验证IP地址等相关信息代码
May 10 Javascript
js实现字符串转日期格式的方法
May 20 Javascript
Bootstrap每天必学之面板
Nov 30 Javascript
详解JavaScript异步编程中jQuery的promise对象的作用
May 03 Javascript
jquery实现点击弹出可放大居中及关闭的对话框(附demo源码下载)
May 10 Javascript
详解JS-- 浮点数运算处理
Nov 28 Javascript
对vue中v-on绑定自定事件的实例讲解
Sep 06 Javascript
bootstrap table列和表头对不齐的解决方法
Jul 19 Javascript
vue 使用外部JS与调用原生API操作示例
Dec 02 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
WHOOPS PHP调试库的使用
2017/09/29 PHP
List the Codec Files on a Computer
2007/06/18 Javascript
jQuery中调用WebService方法小结
2011/03/28 Javascript
jquer之ajaxQueue简单实现代码
2011/09/15 Javascript
jWiard 基于JQuery的强大的向导控件介绍
2011/10/28 Javascript
js弹出模式对话框,并接收回传值的方法
2013/03/12 Javascript
jquery动态增加删除表格行的小例子
2013/11/14 Javascript
JS控制图片翻转示例代码(兼容firefox,ie,chrome)
2013/12/19 Javascript
document节点对象的获取方式示例介绍
2013/12/24 Javascript
node.js中的fs.fchmod方法使用说明
2014/12/16 Javascript
js实现接收表单的值并将值拼在表单action后面的方法
2015/11/23 Javascript
AngularJS模块学习之Anchor Scroll
2016/01/19 Javascript
jquery广告无缝轮播实例
2017/01/05 Javascript
Vue 2中ref属性的使用方法及注意事项
2017/06/12 Javascript
node.js + socket.io 实现点对点随机匹配聊天
2017/06/30 Javascript
详解vue.js+UEditor集成 [前后端分离项目]
2017/07/07 Javascript
JS设计模式之访问者模式定义与用法分析
2018/02/05 Javascript
[09:43]DOTA2每周TOP10 精彩击杀集锦vol.5
2014/06/25 DOTA
Python实现微信公众平台自定义菜单实例
2015/03/20 Python
Python中类似于jquery的pyquery库用法分析
2019/12/02 Python
Django数据结果集序列化并展示实现过程
2020/04/22 Python
python 两种方法修改文件的创建时间、修改时间、访问时间
2020/09/26 Python
在PyCharm中安装PaddlePaddle的方法
2021/02/05 Python
阿里巴巴国际站:Alibaba.com
2016/07/21 全球购物
JBL美国官方商店:扬声器、耳机等
2019/12/01 全球购物
英国家具、照明、家居用品网上商店:Wayfair.co.uk
2020/02/13 全球购物
Whistles官网:英国女装品牌
2020/08/14 全球购物
执行力心得体会
2013/12/31 职场文书
网络宣传方案
2014/03/15 职场文书
《吃水不忘挖井人》教学反思
2014/04/15 职场文书
公司联欢会策划方案
2014/05/19 职场文书
李白故里导游词
2015/02/12 职场文书
自主招生学校推荐信范文
2015/03/26 职场文书
教师“一帮一”结对子活动总结
2015/05/07 职场文书
毕业生的自我鉴定表范文
2019/05/16 职场文书
解析mybatis-plus中的resultMap简单使用
2021/11/23 Java/Android