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>

运行结果:

相关推荐
IT乐手几秒前
Android 获取类成员值的方法
前端
几何心凉5 分钟前
2026数据新规下,远程控制软件安全大起底:ToDesk、向日葵、UU远程谁更能守护企业资产?
前端
IT_陈寒8 分钟前
Redis 缓存击穿 vs 雪崩:5个实战方案让你的系统稳如磐石
前端·人工智能·后端
daols8819 分钟前
vue表格vxe-table实现表头合并,分组表头自定义合并
前端·vue.js·vxe-table
执行部之龙19 分钟前
js手写——防抖
开发语言·前端·javascript
DEMO派20 分钟前
JavaScript数据存储三剑客:Object、Map与WeakMap完全指南
开发语言·前端·javascript
一拳不是超人21 分钟前
半年AI编程实战总结:从工具到心法,让AI成为你的超能力
前端·人工智能·ai编程
阿杜杜不是阿木木21 分钟前
从0到1构建像Claude Code那样的Agent(二):工具
前端·chrome·agent·ai编程·cluade code
cramer_50h24 分钟前
Python的web开发框架Django再次更新
前端·python·django
weixin_66824 分钟前
Clawith 大模型设计逻辑与前端接口架构分析 -AI分析
前端·人工智能·架构