vue中父子组件传值,解决钩子函数mounted只运行一次
网络编程 2021-07-04 14:06www.168986.cn编程入门
这篇文章主要介绍了vue中父子组件传值,解决钩子函数mounted只运行一次的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随长沙网络推广过来看看吧
因为mounted函数只会在html和模板渲染之后会加载一次,但是在子组件中只有第一次的数据显示是正常的,所以需要再增加一个updated函数,在更新之后就可以重新进行取值加载,完成数据的正常显示。
beforCreate(创建之前)
Created(创建之后)
beforMount(载入之前)
Mounted(载入之后)
beforUpdate(更新之前)
Updated(更新之后)
beforDestroy(销毁之前)
Destroyed(销毁之后)
activate(keep-alive组件激活时调用)
deactivated(keep-alive组件停用时调用)
errorCaptured(这个组件的作用是接受子孙组件报错是调用,三个参数 错误对象、错误的组件、错误信息)
父组件向
子组件传值
通过父组件传值调用子组件显示不同的数据
父组件 :conponent.vue
子组件:iconponent.vue
父组件
<template> <div> <span>父组件</span> <iponent-box :id="this.id"></iponent-box> </div> </template> <script> //导入子组件 import iponent from './iponent.vue'; export default { data () { return { id:12 } }, ponents:{ //用来注册子组件的节点 "iponent-box": iponent } } </script> <style> </style>
子组件
<template> <div>子组件</div> </template> <script> export default { updated:{ this.getComponents(); }, mounted:{ this.getComponents(); }, data () { return { } }, methods:{ getComponents(){ this.$http.get("api/getponents/" + this.id); } }, props: ["id"] //接收父组件传递过来的id值 } </script>
补充知识:Vue父子组件传值,子组件数据只更新一次,之后更改父组件的数据,子组件不再更新
我就废话不多说了,大家还是直接看代码吧~
props:{ reportInfo:{ type:Object, default:()=>{} } }, data:function(){ return { cityName :' ', reportId :' ' , } } mounted:function () { var _this = this; //初始化获得数据,之后在watch中监听更新 _this.cityName = _this.reportInfo.cityName; _this.reportId = _this.reportInfo.reportId; }, watch:{ reportInfo(newValue, oldValue) { var _this = this; _this.cityName = newValue.cityName; _this.reportId = newValue.reportId; } }
以上这篇vue中父子组件传值,解决钩子函数mounted只运行一次的操作就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程