本文介绍了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>
- 父组件改变props,子组件如果直接使用props,会触发子组件更新
- 父组件改变props,子组件如果将props放进data中再使用,不会触发子组件更新
- 父组件改变props,子组件如果将props放进computed中再使用,会触发子组件更新
- data,props和computed的变化都会触发组件更新
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“浅谈vue的props,data,computed变化对组件更新的影响”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
2024年11月14日
2024年11月14日
- 黑鸭子2008-今生最爱[首版][WAV+CUE]
- 彭海桐.2000-好好想想你【SONY】【WAV+CUE】
- 群星.1995-亚洲电视剧集主题曲大全·乐作剧2CD【滚石】【WAV+CUE】
- 林强.1992-春风少年兄【魔岩】【WAV+CUE】
- 世界顶级汽车音响试音王《金色的草原》24K金碟[低速原抓WAV+CUE][1G]
- 周深 /Faouzia《解密 电影原声带》[320K/MP3][39.32MB]
- 周深 /Faouzia《解密 电影原声带》[FLAC/分轨][199.46MB]
- 英雄联盟s14亚军队伍是哪支 英雄联盟s14亚军队伍介绍
- 英雄联盟s14夺冠队伍是哪支 英雄联盟s14夺冠队SKT T1队伍介绍
- faker三冠王是哪几个赛季 faker三冠王赛季介绍
- 岩贵-音乐磁场(AI调音)2CD[WAV]
- 童丽《千愁记旧情》HQII头版限量编号2024[低速原抓WAV+CUE]
- 瑞鸣十五周年纪念3[HQCD限量编号头版][低速原抓WAV+CUE]
- 任天堂专利展示新VR外设:或会随同NS继任机型推出
- 博主制作“Switch 2”模型 与现有掌机对比