完美实现vue3异步加载组件

经过几个小时的努力,终于实现了,根据组件名异常加载组件,直接上代码,网上的很多代码方都有坑,先贴出比较坑的代码:

<template>
	<view class="main">		
		<view class="tops">
			<view class="info">
				<view class="item">
					<view class="tit">姓名:</view>
					<view class="txt">{{qyUser.name}}</view>
				</view>
								
			</view>
		</view>
		<view>
		<component :is="dycomp" v-if="isShow"></component>
		</view>
	</view>
</template>
<script setup>
import {  ref, defineAsyncComponent,markRaw } from 'vue';
import { onLoad } from "@dcloudio/uni-app";
import api from '../../api/api.js';
const qyUser = ref({});
//markRaw方法将组件对象标记为非响应式对象
//shallowRef代替ref来创建一个浅响应式对象
const dycomp = ref(null);
const isShow = ref(false);
onLoad(()=>{
	geQyUserInfo();
	loadAsyncComponent("mdIndex");	
});
const loadAsyncComponent = (componentName)=>{
	console.log(componentName);
	dycomp.value = defineAsyncComponent(() =>{
	  import("../components/"+componentName+".vue");
	  }
	);
	isShow.value = true;
	
}
const geQyUserInfo = ()=>{
		console.log(uni.getStorageSync("qyuserid"));
		api.geQyUserInfo({
			"qyuserid":uni.getStorageSync("qyuserid"),
		},(res)=>{
				qyUser.value = res.data.qyUser;	
		});
}
</script>
<style lang="scss" scoped>
	.main {
		background-color: #f5f6f8;
		width: 100%;
		height: 100vh;
		box-sizing: border-box;
		padding: 20rpx;

		.tops {
			background: #fff;
			border-radius: 20rpx;
			padding: 20rpx 20rpx 10rpx;

			.title {
				margin-bottom: 25rpx;
				border-left: 6rpx solid #1296db;
				padding-left: 10rpx;
			}

			.info {

				.item {
					display: flex;
					align-items: center;
					font-size: 26rpx;
					margin-bottom: 10rpx;

					.tit {
						flex: 1;
						color: #666;
					}

					.txt {
						flex: 4;
					}
				}
			}
		}
	}
</style>

最后优化后成功异步加载组件的代码:

<template>
	<view class="main">		
		<view class="tops">
			<view class="info">
				<view class="item">
					<view class="tit">姓名:</view>
					<view class="txt">{{qyUser.name}}</view>
				</view>
								
			</view>
		</view>
		<view>
		<component :is="dycomp" v-if="isShow"></component>
		</view>
	</view>
</template>
<script setup>
import {  ref, defineAsyncComponent,markRaw,shallowRef } from 'vue';
import { onLoad } from "@dcloudio/uni-app";
import api from '../../api/api.js';
const qyUser = ref({});
//markRaw方法将组件对象标记为非响应式对象
//shallowRef代替ref来创建一个浅响应式对象
const dycomp = shallowRef(null);
const isShow = ref(false);
onLoad(()=>{
	geQyUserInfo();
		
});
const loadAsyncComponent = (componentName)=>{
	console.log(componentName);
	dycomp.value = defineAsyncComponent({
		  loader: () => import("../../components/"+componentName+".vue"),
		  delay: 200,
		  timeout: 3000
		}
	);
	isShow.value = true;
	
}
const geQyUserInfo = ()=>{
		api.geQyUserInfo({
			"qyuserid":uni.getStorageSync("qyuserid"),
		},(res)=>{
				qyUser.value = res.data.qyUser;
				loadAsyncComponent("mdIndex");
		});
}
</script>
<style lang="scss" scoped>
	.main {
		background-color: #f5f6f8;
		width: 100%;
		height: 100vh;
		box-sizing: border-box;
		padding: 20rpx;

		.tops {
			background: #fff;
			border-radius: 20rpx;
			padding: 20rpx 20rpx 10rpx;

			.title {
				margin-bottom: 25rpx;
				border-left: 6rpx solid #1296db;
				padding-left: 10rpx;
			}

			.info {

				.item {
					display: flex;
					align-items: center;
					font-size: 26rpx;
					margin-bottom: 10rpx;

					.tit {
						flex: 1;
						color: #666;
					}

					.txt {
						flex: 4;
					}
				}
			}
		}
	}
</style>

相关推荐

  1. 完美实现vue3异步组件

    2024-05-13 21:42:05       38 阅读
  2. Vue3: Suspense异步组件

    2024-05-13 21:42:05       60 阅读
  3. Vue3优化之实现和子组件异步

    2024-05-13 21:42:05       64 阅读
  4. vue 异步组件

    2024-05-13 21:42:05       62 阅读
  5. vue defineAsyncComponent 异步组件

    2024-05-13 21:42:05       60 阅读
  6. Vue-Router: 如何使用异步组件实现路由懒

    2024-05-13 21:42:05       54 阅读
  7. Vue3实现图片懒

    2024-05-13 21:42:05       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-13 21:42:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-13 21:42:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-13 21:42:05       82 阅读
  4. Python语言-面向对象

    2024-05-13 21:42:05       91 阅读

热门阅读

  1. ts 详细-学习

    2024-05-13 21:42:05       30 阅读
  2. notepad++

    notepad++

    2024-05-13 21:42:05      30 阅读
  3. 存储芯片了解

    2024-05-13 21:42:05       34 阅读
  4. 大模型管理工具:Ollama

    2024-05-13 21:42:05       36 阅读
  5. 软件测试至关重要

    2024-05-13 21:42:05       25 阅读
  6. 做跨境电商如何解决IP独立环境?

    2024-05-13 21:42:05       31 阅读
  7. [HDLBits] Three modules

    2024-05-13 21:42:05       34 阅读
  8. 杂记-记一次前端打包问题解决过程

    2024-05-13 21:42:05       27 阅读
  9. python 关键字(in)

    2024-05-13 21:42:05       30 阅读
  10. Buffer

    2024-05-13 21:42:05       35 阅读