vue中父子组件传值,解决钩子函数mounted只运行一次
编程学习 2021-07-04 14:06www.dzhlxh.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>
<icomponent-box :id="this.id"></icomponent-box>
</div>
</template>
<script>
//导入子组件
import icomponent from './icomponent.vue';
export default {
data () {
return {
id:12
}
},
components:{ //用来注册子组件的节点
"icomponent-box": icomponent
}
}
</script>
<style>
</style>
子组件
<template>
<div>子组件</div>
</template>
<script>
export default {
updated:{
this.getComponents();
},
mounted:{
this.getComponents();
},
data () {
return {
}
},
methods:{
getComponents(){
this.$http.get("api/getcomponents/" + 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。
编程语言
- ThinkPad 笔记本如何调节LCD屏幕亮度
- Dreamweaver制作网页打开特效教程
- Win10 Mobile 10586升级后无限重启怎么办 硬重启帮您
- Win8系统提示音频设备有问题有一个或多个音频服
- Xbox One版Win10首个预览版9月份发布
- 如何在textarea文本输入区内实现换行
- Win10 Build 9901系统更新 预览版新版本下载
- McAfee Framework存在远程格式串处理漏洞
- Win10家庭版今日(7月30)正式在中国官方商城开卖
- Win10 Mobile预览版更新完10536.1000后才收到10536.100
- Win10 RS2更新了什么-Win10 RS2最终版本号1704首曝
- Windows7如何查看回收站对应的文件夹有哪些方法
- Win10让Charms栏回归桌面的方法教程
- 取消Windows XP系统开机启动画面的小技巧
- win8系统怎么下载安装USB百兆网卡?
- XP系统下磁盘空间变少了怎么办?XP系统磁盘空间