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

在网站开发中如何设置用户登录网站查看

在网站开发中如何设置用户登录,网站查看,网站开发 运行及维护,永登县建设局网站在 Vue3 中实现两个组件的动态切换保活&#xff0c;核心是通过 <component> 动态组件与 <KeepAlive> 缓存组件的组合使用。以下是具体实现方案和进阶技巧&#xff1a; 一、基础实现方案 1. 动态组件 KeepAlive 保活 使用 <component :is> 实现动态切换&am…

在 Vue3 中实现两个组件的动态切换保活,核心是通过 <component> 动态组件与 <KeepAlive> 缓存组件的组合使用。以下是具体实现方案和进阶技巧:


一、基础实现方案

1. 动态组件 + KeepAlive 保活

使用 <component :is> 实现动态切换,并用 <KeepAlive> 包裹以实现状态缓存:

<template><div><button @click="toggleComponent">切换组件</button><KeepAlive><component :is="currentComponent" /></KeepAlive></div>
</template><script setup>
import { shallowRef } from 'vue'
import CompA from './CompA.vue'
import CompB from './CompB.vue'const components = { CompA, CompB }
const currentComponent = shallowRef(CompA) // 使用 shallowRef 避免深度响应式const toggleComponent = () => {currentComponent.value = currentComponent.value === CompA ? CompB : CompA
}
</script>

关键点

  • shallowRef 替代 ref:避免对组件对象进行深度响应式转换,减少性能开销
  • KeepAlive 包裹:缓存组件实例,保留表单输入、滚动位置等状态

二、高级配置技巧

1. 精准控制缓存范围

通过 include/exclude 指定需缓存的组件名:

<KeepAlive include="CompA,CompB" max="2"><component :is="currentComponent" />
</KeepAlive>

参数说明

  • include:字符串/正则/数组,匹配组件 name 属性
  • max:最大缓存数(超过时按 LRU 算法淘汰旧实例)
2. 生命周期管理

利用专属钩子处理缓存状态:

<script setup>
import { onActivated, onDeactivated } from 'vue'onActivated(() => {console.log('组件激活:恢复定时器/网络请求')timer = setInterval(fetchData, 5000)
})onDeactivated(() => {console.log('组件休眠:清理资源')clearInterval(timer)
})
</script>

对比常规生命周期

场景常规组件被缓存组件
首次加载created → mountedcreated → mounted → activated
离开beforeUnmount → unmounteddeactivated
再次进入重新创建activated

三、常见问题解决

1. 组件未缓存
  • 原因:组件未声明 name 属性
  • 解决方案
    <!-- CompA.vue -->
    <script>
    export default { name: 'CompA' }
    </script><!-- 或使用 setup 语法糖 -->
    <script setup>
    defineOptions({ name: 'CompA' }) // 需安装 unplugin-vue-define-options 插件
    </script>
    
2. 动态切换卡顿
  • 优化方案
    • 使用异步组件加载(结合 defineAsyncComponent
    • 添加 CSS 过渡动画:
      .fade-enter-active, .fade-leave-active {transition: opacity 0.3s;
      }
      .fade-enter-from, .fade-leave-to {opacity: 0;
      }
      
3. 表单状态丢失
  • 强制刷新缓存:通过 key 属性重置组件
    <KeepAlive><component :is="currentComponent" :key="customKey" />
    </KeepAlive>
    
    切换时更新 customKey 值即可触发重新渲染。

四、性能优化建议

  1. 按需缓存:仅对高频切换或状态复杂的组件使用缓存
  2. 内存控制:设置 max 属性避免内存溢出(推荐值:3-5)
  3. 数据持久化:在 deactivated 时保存状态至 localStorage:
    onDeactivated(() => {localStorage.setItem('formData', JSON.stringify(formState))
    })
    

五、完整代码示例

<template><div class="container"><div class="switch-buttons"><button v-for="(comp, name) in components" :key="name"@click="currentComponent = comp":class="{ active: currentComponent === comp }">{{ name }}</button></div><KeepAlive :max="3"><Transition name="fade" mode="out-in"><component :is="currentComponent" /></Transition></KeepAlive></div>
</template><script setup>
import { shallowRef } from 'vue'
import CompA from './CompA.vue'
import CompB from './CompB.vue'const components = { CompA, CompB }
const currentComponent = shallowRef(CompA)
</script><style scoped>
.switch-buttons {margin-bottom: 20px;
}
button {padding: 8px 16px;margin-right: 10px;&.active {background: #1890ff;color: white;}
}
.fade-enter-active, .fade-leave-active {transition: opacity 0.3s;
}
.fade-enter-from, .fade-leave-to {opacity: 0;
}
</style>

扩展应用场景

  • 仪表盘模块切换(保留图表缩放状态)
  • 多步骤表单(缓存已填写步骤)
  • 选项卡式数据展示(保持分页器位置)

通过合理使用动态组件与 KeepAlive,可显著提升复杂交互场景下的用户体验。建议结合 Vue Devtools 观察组件缓存状态,针对性优化关键路径性能。

http://www.dtcms.com/a/593379.html

相关文章:

  • SAP FICO资产主数据创建接口
  • 『 QT 』显示类控件 (一)
  • 网站建设彳金手指排名wordpress电子书插件
  • 石狮网站建设科技vmware做网站步骤
  • 全国网站建设公司排名泰安市人才交流服务中心
  • C++双向链表删除操作:由浅入深完全指南
  • 云手机轻松玩转网络手游
  • 手机拍照明晰度评估:传感器尺寸像素数量与处理器算法解析
  • Web VIVO手机商城网站项目4页面
  • 【杂谈】-人工智能时代的基础设施变革:引领未来十年科技发展的关键
  • 有关网络技术的网站iis7 建立网站
  • vue提交代码报错---eslint检查
  • 天津外贸公司网站制作wordpress中国区官方论坛
  • 成都企业网站商城定制网络推广方案包括哪些内容
  • 商城网站建设机构怎样做展示型网站
  • Rust类型系统奇技淫巧:幽灵类型(PhantomData)——理解编译器与类型安全
  • Visual Studio Code 之C/C++开发编译环境搭建
  • 长沙网站制造太原网站建设注意
  • PortSwigger靶场之SSRF with whitelist-based input filter通关秘籍
  • 太原网站快速排名提升手机商城网站制作公司
  • Redis 核心命令速查表
  • 中药饮片批发市场如何提升产品质量以迎合客户需求?
  • k8s中应用容器随redis集群自动重启
  • C语言结构体入门与实践:打造你的“数据百宝箱”
  • Docker核心概念与实战指南
  • 视频网站开发问题整人网站建设
  • 【Java】面向对象编程
  • 作业11.9
  • 西安网站开发公司网站服务器租用价格
  • 北京市城乡结合部建设领导小组办公室网站聊城做网站的公司效果