当前位置: 首页 > news >正文

完美实现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>
http://www.lryc.cn/news/344415.html

相关文章:

  • 点云成图原理
  • 如何将jsp项目转成springboot项目
  • C语言:环形链表
  • typescript综合练习1(展开音乐播放列表)
  • 零基础入门学习Python第二阶02面向对象,迭代器生成器,并发编程
  • Unity | Shader基础知识(第十三集:编写内置着色器阶段总结和表面着色器的补充介绍)
  • JavaScript map对象/set对象详解
  • 【kettle017】kettle访问DB2数据库并处理数据至execl文件(最近完善中)
  • Spring Cloud原理详解和作用特点
  • Linux —— 进程间通信
  • ASP.NET信息安全研究所设备管理系统的设计与实现
  • <网络安全>《81 微课堂<安全产品微简介(1)>》
  • 【6D位姿估计】FoundationPose 跑通demo 训练记录
  • Python 中 “yield“ 的不同行为
  • 迅睿CMS中实现关键词搜索高亮
  • 晶振的精度与稳定性有什么关系?
  • 【C】137 只出现一次的数字
  • 51单片机入门:DS1302时钟
  • Redis-5 分布式锁
  • 音转文工具,9.8k star! 【送源码】
  • 【首次发布】华为 OD 机试 C卷抽中题库清单(真题库),目前华为OD机考以C卷为主,特殊情况会发送D卷
  • 【进程等待】waitpid的参数pid | status的位图位操作WIFEXITEDWEXITSTATUS宏
  • unity---常用API
  • 设计模式: 模板模式
  • [虚拟机+单机]梦幻契约H5修复版_附GM工具
  • 头文件相互包含 前向声明
  • 七款好用的上网行为管理软件推荐 |有没有好用的上网行为管理系统
  • centos7-bcc 安装
  • 5.06号模拟前端面试8问
  • 解读Inscode AI:开启代码智能化的新时代