【前端小点】vue3项目内根据主题读取不同文件夹下的图片资源(图片文件)

项目要求实现一键换肤的功能,不仅仅是主题颜色上的替换,还有图片素材的替换,主题颜色替换的方案大同小异,下面仅对图片素材的一件替换进行方法描述。

主要思路

使用本地仓库对当前主题进行存储,系统根据主题去加载不同文件夹下的素材文件。

实现方式

1、动态读取文件夹下的图片文件。

注意:不同的素材我们用的是不同文件夹,但是文件名字是一样的,存储位置也是相对统一的,如下图所示。例如:暗色主题下有一个图片地址为bg.jpg,则亮色主题下也需要有一个图片地址为bg.jpg
在这里插入图片描述
编写js/ts对文件内容进行读取并存储,我的文件位置: src/themes.js
注意: import.meta.glob必须是静态字符串,不可设置为动态获取的.

themes.js
// 获取文件夹下所有文件
const images = import.meta.glob(`./assets/themes/light/**/**/*.*`)
const commonImages = import.meta.glob(`./assets/themes/common/**/**/*.*`)let prefix = './assets/themes/light/'
let commonPrefix = './assets/themes/common/'/**** @param {*} imagePaths* @returns*/
const handleFileName = (imagePaths, pathPrefix) => {console.log(imagePaths)let pathObj = {}Object.keys(imagePaths).forEach((key) => {key = key.replace(pathPrefix, '')let pathArr = key.split('/')let length = pathArr.length// 名字let name = pathArr[length - 1]// 文件夹let folder = ''if (length >= 2) {folder = pathArr[length - 2]}// 父文件夹let folderParent = ''if (length >= 3) {folderParent = pathArr[length - 3]}pathObj[key] = {name,folder,folderParent}})return pathObj
}// 获取文件名和文件夹名
let files = handleFileName(images, prefix)
console.log(files)
let commonFiles = handleFileName(commonImages, commonPrefix)const themes = {light: {},dark: {},common: {}
}// 设置不同主题下的图片 文件夹/文件
Object.keys(files).forEach((key) => {let fileObj = files[key]let { name, folder, folderParent } = fileObjlet lightHref = new URL(`./assets/themes/light/${key}`, import.meta.url).hreflet darkHref = new URL(`./assets/themes/dark/${key}`, import.meta.url).hrefif (folderParent) {themes.light[folderParent] ? {} : themes.light[folderParent] = {}themes.light[folderParent][folder] ? {} :themes.light[folderParent][folder]themes.light[folderParent][folder][name] = lightHrefthemes.dark[folderParent] ? {} : themes.dark[folderParent] = {}themes.dark[folderParent][folder] ? {} : themes.dark[folderParent] = {}themes.dark[folderParent][folder][name] = darkHref} else if (folder) {themes.light[folder] ? {} : themes.light[folder] = {}themes.light[folder][name] = lightHrefthemes.dark[folder] ? {} : themes.dark[folder] = {}themes.dark[folder][name] = darkHref} else {themes.light[name] = lightHrefthemes.dark[name] = darkHref}
})// 设置公共图片文件
Object.keys(commonFiles).forEach((key) => {let fileObj = commonFiles[key]let { name, folder, folderParent } = fileObjlet commonHref = new URL(`./assets/themes/common/${key}`, import.meta.url).hrefif (folderParent) {themes.common[folderParent] ? {} : themes.common[folderParent] = {}themes.common[folderParent][folder] = {}themes.common[folderParent][folder][name] = commonHref} else if (folder) {themes.common[folder] ? {} : themes.common[folder] = {}themes.common[folder][name] = commonHref} else {themes.common[name] = commonHref}
})export default themes

2、对项目内的主题(暗色\亮色)进行仓库存储

这里我使用的是pinia进行本地数据存储,我的文件位置src/pinia/modules/theme.ts

import { defineStore } from 'pinia'
import { ref } from 'vue'
import themesImage from '@/themes'const useThemeStore = defineStore('themeStore', () => {let theme = ref('light')const toggleTheme = () => {theme.value = theme.value === 'light' ? 'dark' : 'light'}const getThemeImage = (imgPath: string) => {return getImage(theme.value, imgPath)}const getCommonImage = (imgPath: string) => {return getImage('common', imgPath)}const getImage = (themeName: string, imgPath: string) => {let imagePath = imgPath.split('/')imagePath.lengthif (imagePath.length == 1) {return themesImage[themeName][imagePath[0]]} else if (imagePath.length == 2) {return themesImage[themeName][imagePath[0]][imagePath[1]]} else {return themesImage[themeName][imagePath[0]][imagePath[1]][imagePath[2]]}}return {theme,toggleTheme,getThemeImage,getCommonImage}
})export default useThemeStore

3、编写工具类获取图片地址

编写工具类,便于后续界面读取文件,我的文件地址为: src/utils/themes.ts

import useThemeStore from '@/pinia/modules/theme'
const themeStore = useThemeStore()// 获取主题图片
export const getThemeImage = themeStore.getThemeImage
// 获取公共图片
export const getCommonImage = themeStore.getCommonImage

4、在vue组件中使用

做好准备工作以后,我们只需在组件中引入我们的工具类即可。

<template><div class="container"><img :src="themeUtils.getThemeImage('index/logo.png')" alt="" class="img" /><img :src="themeUtils.getCommonImage('logo.png')" alt="" class="img" /><el-button @click="themeUtils.toggleTheme">一键换肤</el-button></div>
</template><script lang="ts" setup>
import * as themeUtils from '@/utils/themes'
</script><style lang="scss" scoped>
.img{border: 1px solid #ccc;border-radius: 10px;margin-right: 10px;
}
.container{display: flex;align-items: center;
}
</style>

效果如下:
在这里插入图片描述
点击一键换肤以后
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/23230.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vxe-table实现动态列

vxe-table实现动态列 1.动态列解释2.解决步骤2.1将后端返回的动态列表头&#xff0c;按照格式拼接在固定列表头上2.2将后端返回的列表数据按照键值对格式组装 1.动态列解释 正常列表是有固定的列&#xff1b;我的需求是&#xff0c;最初只知道表格的固定两列&#xff0c;查询数…

Windows 11 使用容器(Docker Podman)

文章目录 背景1、相关网站1.1、WSL1.2、Docker1.3、Podman 2、环境3、安装部署3.1、安装 WSL3.2、Docker3.2.1、Docker Desktop3.2.1.1、安装3.2.1.2、拉取镜像3.2.1.3、启动容器 3.3、Podman3.3.1、安装3.3.2、使用3.3.3、异常处理 总结 背景 Windows 系统中使用容器&#xf…

UE_C++ —— Gameplay Modules

目录 一&#xff0c;Module Creation INI File Setup 二&#xff0c;Multiple Gameplay Modules 三&#xff0c;Limitations 编译成 DLL 的游戏相关类的集合&#xff1b;正如引擎本身由一组模块构成一样&#xff0c;每个游戏也是由一个或多个游戏模块构成的&#xff1b;这些…

蓝桥杯定时器实现led闪烁

step1.配置定时器&#xff0c;TIM1时高级定时&#xff0c;TIM2是通用定时器&#xff0c;用TIM2就行&#xff0c;用内部时钟源&#xff0c;记住相关公式&#xff0c;定时器中断配置时要使能&#xff0c;且生成代码后也要在mian中写使能函数 step2.写代码 配置生成代码后多出的…

二:前端发送POST请求,后端获取数据

接着一&#xff1a;可以通过端口访问公网IP之后 二需要实现&#xff1a;点击飞书多维表格中的按钮&#xff0c;向服务器发送HTTP请求&#xff0c;并执行脚本程序 向服务器发送HTTP请求&#xff1a; 发送请求需要明确一下几个点 请求方法&#xff1a; 由于是向服务器端发送值…

内外网文件传输 安全、可控、便捷的跨网数据传输方案

一、背景与痛点 在内外网隔离的企业网络环境中&#xff0c;员工与外部协作伙伴&#xff08;如钉钉用户&#xff09;的文件传输面临以下挑战&#xff1a; 安全性风险&#xff1a;内外网直连可能导致病毒传播、数据泄露。 操作繁琐&#xff1a;传统方式需频繁切换网络环境&…

elasticsearch在windows上的配置

写在最前面&#xff1a; 上资源 第一步 解压&#xff1a; 第二步 配置两个环境变量 第三步 如果是其他资源需要将标蓝的文件中的内容加一句 xpack.security.enabled: false 不同版本的yaml文件可能配置不同&#xff0c;末尾加这个 xpack.security.enabled: true打开bin目…

OpenCV二值化处理

1.1. 为什么需要二值化操作 二值化操作将灰度图像转换为黑白图像&#xff0c;即将图像中的像素值分为两类&#xff1a;前景&#xff08;通常为白色&#xff0c;值为 255&#xff09;和背景&#xff08;通常为黑色&#xff0c;值为 0&#xff09;。二值化的主要目的是简化图像&…

[Android]浏览器下载的apk文件无法识别无法安装问题

在Android电话机上&#xff0c;用浏览器下载apk进行版本更新&#xff0c;出现下载文件没被识别为apk&#xff0c;导致无法安装问题。 原来的下载链接&#xff1a; https://mojsetup.obs.cn-southwest-2.myhuaweicloud.com/callphone-release-1.0.4.apk 修改后的下载链接&…

如何使用SSH连接设备?很简单!

前言 小白发现最近写的文章都与SSH息息相关&#xff0c;于是就有了这一篇文章&#xff0c;免得在后续的文章又不断重复如何SSH连接设备。 有需要的小伙伴自然就会看到这里&#xff0c;也不会影响到其他小伙伴的阅读体验。 至于文章里的广告嘛……就当是小伙伴们给小白的一点…

【自学嵌入式(9)ESP8266网络服务器的使用】

ESP8266网络服务器的使用 ESP8266WiFi 库① WiFiClass② WiFiClient③ WiFiServer④ WiFiUDP ESP8266WiFiMulti 库① WiFiMulti ESP8266WebServer 库① P8266WebServer 网络服务器实例在浏览器中控制ESP8266指示灯将开发板引脚状态显示在网页中 在之前的文章中&#xff0c;曾经…

pytorch3d安装记录

官方安装教程&#xff1a; https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md 通过pip 或conda 可以很容易安装上预编译好的包&#xff0c; 安装过程不会报错&#xff0c; 但是使用的时候就会报各种错误 &#xff0c;原因是预编译好的包跟自己的环境不一定…

【CVPR2024-工业异常检测】PromptAD:与只有正常样本的少样本异常检测的学习提示

代码链接 摘要 摘要写作总结&#xff1a; 1.提出 两个关键点 &#xff08;视觉语言模型【模型】 少量工业异常检测【方向】&#xff09; 2.想要解决的问题 3.针对上述问题&#xff0c;本文提出了一种什么【方法】的什么【应用方面】方法【模型名】 4.具体讲方法的步骤 5.实验…

【PostgreSQL】如何通过调整PostgreSQL配置参数提高数据库性能

如何通过调整PostgreSQL配置参数提高数据库性能 1. 数据库初始性能2. 内存相关参数3. WAL&#xff08;Write-Ahead Logging&#xff09;相关参数4. 并行查询相关参数5. 连接相关参数6. 根据情况调整 1. 数据库初始性能 使用pgbench在更新PostgreSQL数据库配置前先测试下数据库…

AI Agent实战:打造京东广告主的超级助手 | 京东零售技术实践

前言 自2022年末ChatGPT的问世&#xff0c;大语言模型&#xff08;LLM&#xff09;技术引发全球关注。在大模型技术落地的最佳实践中&#xff0c;智能体&#xff08;Agent&#xff09;架构显现出巨大潜力&#xff0c;成为业界的普遍共识&#xff0c;各大公司也纷纷启动Agent技…

从【人工智能】到【计算机视觉】,【深度学习】引领的未来科技创新与变革

前几天偶然发现了一个超棒的人工智能学习网站&#xff0c;内容通俗易懂&#xff0c;讲解风趣幽默&#xff0c;简直让人欲罢不能。忍不住分享给大家&#xff0c;点击这里立刻跳转&#xff0c;开启你的AI学习之旅吧&#xff01; 前言 – 人工智能教程https://www.captainbed.cn/l…

ChātGPT赋能的“SolidWorks工具箱”:重塑3D设计效率新标杆

ChātGPT精心打造的“SolidWorks工具箱”正逐步成为3D设计领域中的一颗璀璨新星&#xff0c;其集高效、便捷与创新于一身&#xff0c;为用户带来了前所未有的设计体验。以下是对这一革命性工具箱的深度剖析与美化呈现&#xff1a; 一、核心功能&#xff1a;重塑设计流程&#x…

php处理图片出现内存溢出(Allowed memory size of 134217728 bytes exhausted)

错误&#xff1a; 最近做图片上传功能时发现上传某些图片时报内存溢出错误。如下所示&#xff1a; {"code": 0,"msg": "Allowed memory size of 134217728 bytes exhausted (tried to allocate 24576 bytes)","data": {"code&q…

Spring——SpringSecurity开发经验实战

摘要 本文介绍了一个简单的 Spring Security 实战示例&#xff0c;涵盖基本的身份验证和授权流程。首先介绍了 Spring Security 是一个强大的安全框架&#xff0c;用于在 Spring 应用中实现身份验证、授权以及保护应用免受常见安全攻击。接着详细阐述了项目结构、添加 Spring …

Innovus中快速获取timing path逻辑深度的golden脚本

在实际项目中我们经常会遇到一条timing path级数特别多&#xff0c;可能是一两页都翻不完。此时&#xff0c;我们大都需要手工去数这条path上到底有哪些是设计本身的逻辑&#xff0c;哪些是PR工具插入的buffer和inverter。 数字IC后端手把手培训教程 | Clock Gating相关clock …