Vue(uniapp)父组件方法和子组件方法执行优先顺序

涉及到的知识点:钩子函数mounted和created的区别:先看问题,父组件从后端通过$ajax获取数据,在将父组件将值传输给子组件,使用子组件使用created钩子函数获取数据,按自己的想法应该是父组件先获取后端数据,在传入给子组件,可是事实是,子组件先获取数据,父组件在从后端获取数据,这样的话子组件就没有数据获取为空。

父页面:

复制代码
<template>
	<view class="approval-datails">
		<view class="information"> <!--申请信息-->
			<uni-collapse ref="collapse" @change="change">
				<uni-collapse-item title="审批流程" title-border="show" :open="true">
					<uni-stepinformation :instanceApplyList='instanceApplyList'></uni-stepinformation>
				</uni-collapse-item>
			</uni-collapse>
		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
			
				instanceApplyList:[],//传给子页面的数组
				
			}
			
		},
		
		
		
		methods: {
		 getWaitingTask(){
				this.$ajax.getWaitingTask({
					id: this.id
				}, (response) => {
					if (response.success === true) {
						console.log('responseswaiting', response)
						this.result = response.result
						this.instanceApplyList = response.result.instanceApplyList
						// console.log('responseswaiting', this.result)
						console.log('this.instanceApplyList',this.instanceApplyList)
					}
					// this.listType(response)
				
				});
			},
		
		}
	}
</script>

子页面:

解决方法:将子页面中钩子函数created变成mounted调用type函数方法,这样就可以让父页面的函数先运行获取后端数据,在吧数据传入子页面,子页面获取数据在输出。

复制代码
<template>
	<view class="box">
		
	</view>
</template>

<script>
	export default {
		props:{
			instanceApplyList: Array,
			default:() => []
		},
		data() {
			return {
				judge:[],
				
			}
		},
		// created() {
		// 	this.type();
			
		// },
		mounted() {
			this.type()
		},
		methods: {
			type(){
				this.judge=this.instanceApplyList
				console.log('this.judge',this.judge) 
				
			}
		}
	}
</script>

运行结果:

相关推荐
JosieBook33 分钟前
【Vue】15 Vue技术——Vue计算属性简写:提升代码简洁性的高效实践
前端·javascript·vue.js
rfidunion1 小时前
springboot+VUE+部署(11。Nginx)
linux·vue.js·nginx
weixin199701080161 小时前
Lazada商品详情页前端性能优化实战
前端·性能优化
星火开发设计2 小时前
异常规范与自定义异常类的设计
java·开发语言·前端·c++
CappuccinoRose2 小时前
CSS 语法学习文档(十一)
前端·css·学习·表单控件
海兰2 小时前
Elastic Stack 9.3.0 日志探索
java·服务器·前端
输出输入3 小时前
Java Swing和JavaFX用哪个好
java·前端
铁甲前沿3 小时前
一个月玩转MQTT(篇五:开发自己的MQTT WEB页面)
前端·mqtt·asp.net·mqtt web开发
~央千澈~4 小时前
抖音弹幕游戏开发之第7集:识别不同类型的消息·优雅草云桧·卓伊凡
java·服务器·前端
拾荒李4 小时前
在 Vue 项目里“无痛”使用 React 组件:以 Veaury + Vite 为例
前端·vue.js·react.js