在element-plus的Dialog组件中使用el-loading效果

最近遇到一个需求,在页面中有个组件,子组件由el-dialog包裹,希望展示隐藏el-dialog时有加载动画,加载动画用的是v-loading。

javascript 复制代码
//父组件
<template>
	<tabs v-model="tabsVisible"/>
</template>
<script setup>
	const tabsVisible = ref(false);
</script>
javascript 复制代码
//子组件
<template>
	<el-dialog
		v-model="modalVisible"
		title=""
		:close-on-click-modal="false"
		width="85%"
		v-loading="loading"
	>
		<!--> 这里是展示的内容 <-->
	</el-dialog>
</template>
<script setup>
	import { ref, computed, watch } from "vue";
	const loading = ref(false);
	const props = defineProps({
		//接收参数
		modelValue: false,		
	});
	const emit = defineEmits(["update:modelValue"]);
	const modalVisible = computed({
		get() {
			return props.modelValue;
		},
		set(value) {
			emit("update:modelValue", value);
		},
	});
</script>

如果直接在el-dialog上面加v-loading那么进入页面会报错,我查了下大概意思是:在具有非元素根节点的组件上使用的运行时指令,这些指令将无法按预期发挥作用(Runtime directive used on component with non-element root node. The directives will not function as intended. )。

这个时候我换了种方式,使用element-plus提供的loading指令。

javascript 复制代码
//子组件
<template>
	<el-dialog
		v-model="modalVisible"
		title=""
		:close-on-click-modal="false"
		width="85%"
	>
		<!--> 这里是展示的内容 <-->
	</el-dialog>
</template>
<script setup>
	import { ref, computed, watch } from "vue";
	const props = defineProps({
		//接收参数
		modelValue: false,		
	});
	const emit = defineEmits(["update:modelValue"]);
	const modalVisible = computed({
		get() {
			return props.modelValue;
		},
		set(value) {
			emit("update:modelValue", value);
		},
	});
	watch(modalVisible, (newVal, oldVal) => {
		if (newVal) {
			initData();
		} else {
			//处理数据
		}
	});
	const initData = async () => {
		//获取数据
		let loading = null;
		try {
			setTimeout(() => {
			 	loading = ElLoading.service({ fullscreen: true });
			}, 30);
			let res = await getInfo();
		} catch (e) {
			console.error(e);
		}
		loading.close();
	};
	
</script>

当然如果不是父子组件,直接用v-loading就行了。

相关推荐
用户新2 小时前
JS事件深度解析四 事件的循环和异步
前端·javascript·事件·event loop
广州灵眸科技有限公司8 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) Easy-Eai编译环境准备与更新
服务器·前端·人工智能·python·深度学习
万少9 小时前
我把 Kimi 接进微信,几分钟做了个随手出图助手
前端
xiaofeichaichai9 小时前
网络请求与实时通道
前端·网络
kTR2hD1qb9 小时前
从 Responses API 到 Chat Completions:一个模型网关的设计复盘
linux·前端
kyriewen11 小时前
浏览器缓存最强攻略:强缓存、协商缓存、CDN、更新策略,一篇搞定
前端·面试·浏览器
持敬chijing11 小时前
Web渗透之SQL注入-联合查询注入-注入点数据类型判断
前端·sql·安全·web安全·网络安全·安全威胁分析
卷帘依旧12 小时前
Web3前端一面
前端
古韵12 小时前
告别手写分页逻辑:usePagination 从 50 行到 3 行
java·前端