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>

运行结果:

相关推荐
持续前行3 分钟前
通过 npm 下载node_modules 某个依赖 ;例如 下载 @rollup/rollup-linux-arm64-gnu
前端·javascript·vue.js
chenyingjian37 分钟前
鸿蒙|能力特性-统一文件预览
前端·harmonyos
毛骗导演37 分钟前
OpenClaw 沙箱执行系统深度解析:一条 exec 命令背后的安全长城
前端·架构
天才聪44 分钟前
鸿蒙开发vs前端开发1-父子组件传值
前端
卡尔特斯1 小时前
Android Studio 代理配置指南
android·前端·android studio
李剑一1 小时前
同样做缩略图,为什么别人又快又稳?踩过无数坑后,我总结出前端缩略图实战指南
前端·vue.js
Jolyne_1 小时前
Taro样式重构记录
前端
恋猫de小郭1 小时前
Google 开源大模型 Gemma4 怎么选,本地跑的话需要什么条件?
前端·人工智能·ai编程
文心快码BaiduComate1 小时前
Comate搭载GLM-5.1:长程8H,对齐Opus 4.6
前端·后端·架构
熊猫钓鱼>_>1 小时前
AI驱动的Web应用智能化:WebMCP、WebSkills与WebAgent的融合实践
前端·人工智能·ai·skill·webagent·webmcp·webskills