浅谈vue的props,data,computed变化对组件更新的影响
网络编程 2021-07-04 17:33www.168986.cn编程入门
本篇文章主要介绍了浅谈vue的props,data,puted变化对组件更新的影响,长沙网络推广觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧
本文介绍了vue的props,data,puted变化对组件更新的影响,分享给大家,废话不多说,直接上代码
/ 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' } }, ponents: { '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' } }, puted: { 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放进puted中再使用,会触发子组件更新
- data,props和puted的变化都会触发组件更新
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程