动态计算加载图片

学习啦

别名路径:①npm install path --save-dev②配置

// vite.config,js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'import { viteStaticCopy } from 'vite-plugin-static-copy'
import path from 'path'
export default defineConfig({resolve: {alias: {'@': path.resolve(__dirname, './src') // 使用 Vite 的 resolve 方法}},plugins: [vue(),],server: {fs: {// 允许从一个级别到项目根目录提供文件allow: ['..']},middlewareMode: false,proxy: {'/api': {// 指定目标服务器的地址target: 'http://baidu.com',// changeOrigin: 设置为 true 时,会修改请求头中的 host 字段为目标服务器的主机名,这有助于解决某些服务器对 host 头的严格检查changeOrigin: true,// 用于重写请求路径。这里将所有以 /api 开头的路径重写为空字符串,即去掉 /api 前缀。pathRewrite: { '^/api': '' },// 添加日志记录 设置为 debug 可以在控制台中输出详细的代理日志,帮助调试。logLevel: 'debug', // 添加错误处理回调函数,捕获代理请求中的错误,并返回适当的错误响应onError: (err, req, res) => {console.error('Proxy error:', err);res.status(500).send('Proxy error: ' + err.message);}}},}
})

一、 使用 switch 语句和 Vue 的动态类绑定

<template><div :class="['a-bg', titleClass]"></div>
</template><script setup>
import { ref, computed } from "vue";
// 随机赋值
// 定义一个数组,包含可能的值
const possibleValues = ['one', 'two', 'three'];// 生成一个随机索引
const randomIndex = Math.floor(Math.random() * possibleValues.length);// 根据随机索引设置 systemTitle 的值
const systemTitle = ref(possibleValues[randomIndex]);const titleClass = computed(() => {switch (systemTitle.value) {case 'one':return 'one';case 'two':return 'two';default:return 'three';}
});</script><style lang="scss" scoped>
#app {width: 100%;height: 100vh;
}.a-bg {width: 200px;height: 200px;background: linear-gradient(45deg, #fff, red);position: relative;/* 确保伪元素相对于父元素定位 */&::before {content: '';display: block;/* 设置伪元素的显示方式 */position: absolute;/* 确保伪元素绝对定位 */top: 0;left: 0;// src\assets\image\cat1.jpgbackground: url("/src/assets/image/cat1.jpg") no-repeat center;background-size: 100% 100%;width: 200px;height: 200px;}
}.one::before {background: url("/src/assets/image/cat1.jpg") no-repeat center;background-size: 100% 100%;
}.two::before {// 绝对路径 从项目的根目录开始计算background: url("/src/assets/image/cat2.jpg") no-repeat center;background-size: 100% 100%;
}.three::before {// 别名路径 用了 @ 别名,指向 src 目录下的路径background: url("@/assets/image/cat3.jpg") no-repeat center;background-size: 100% 100%;
}
</style>

二、使用classList.add()和ref实例

<template><div ref="titleImage" class='a-bg'></div>
</template><script setup>
import { ref, watch, onMounted } from "vue";// 随机赋值
const possibleValues = ['one', 'two', 'three'];
const randomIndex = Math.floor(Math.random() * possibleValues.length);
const systemTitle = ref(possibleValues[randomIndex]);const titleImage = ref(null);const updateTitleImageClass = () => {if (titleImage.value) {console.log('Before:', titleImage.value.className); // 打印当前类名titleImage.value.className = 'a-bg ' + systemTitle.value; // 直接设置类名// titleImage.value.classList.add(`${systemTitle.value}`);console.log('After:', titleImage.value.className); // 打印替换后的类名}
};onMounted(() => {console.log(systemTitle.value, 'systemTitle');updateTitleImageClass();
});// 监听 systemTitle 的变化
watch(systemTitle, updateTitleImageClass);
</script><style lang="scss" scoped>
#app {width: 100%;height: 100vh;
}.a-bg {width: 200px;height: 200px;background: linear-gradient(45deg, #fff, red);position: relative;&::before {content: '';display: block;position: absolute;top: 0;left: 0;background: url("/src/assets/image/cat1.jpg") no-repeat center;background-size: 100% 100%;width: 200px;height: 200px;}
}.one::before {background: url("/src/assets/image/cat1.jpg") no-repeat center;background-size: 100% 100% !important;
}.two::before {background: url("/src/assets/image/cat2.jpg") no-repeat center;background-size: 100% 100% !important;
}.three::before {background: url("@/assets/image/cat3.jpg") no-repeat center;background-size: 100% 100% !important;
}
</style>

三、对象映射(类名和图片)

<template><div ref="titleImage" :class="['a-bg', systemTitle.value]"></div>
</template><script setup>
import { ref, watch, onMounted } from "vue";// 随机赋值
const possibleValues = ['one', 'two', 'three'];
const randomIndex = Math.floor(Math.random() * possibleValues.length);
const systemTitle = ref(possibleValues[randomIndex]);const titleImage = ref(null);// 对象映射
const classMapping = {one: 'cat1',two: 'cat2',three: 'cat3'
};const updateTitleImageClass = () => {if (titleImage.value) {console.log('Before:', titleImage.value.className); // 打印当前类名// 清除所有可能的类名for (const key in classMapping) {titleImage.value.classList.remove(key);}// 添加新的类名titleImage.value.classList.add(systemTitle.value);console.log('After:', titleImage.value.className); // 打印替换后的类名}
};onMounted(() => {console.log(systemTitle.value, 'systemTitle');updateTitleImageClass();
});// 监听 systemTitle 的变化
watch(systemTitle, updateTitleImageClass);
</script><style lang="scss" scoped>
#app {width: 100%;height: 100vh;
}.a-bg {width: 200px;height: 200px;background: linear-gradient(45deg, #fff, red);position: relative;&::before {content: '';display: block;position: absolute;top: 0;left: 0;background: url("/src/assets/image/cat1.jpg") no-repeat center;background-size: 100% 100%;width: 200px;height: 200px;}
}.one::before {background: url("/src/assets/image/cat1.jpg") no-repeat center;background-size: 100% 100% !important;
}.two::before {background: url("/src/assets/image/cat2.jpg") no-repeat center;background-size: 100% 100% !important;
}.three::before {background: url("@/assets/image/cat3.jpg") no-repeat center;background-size: 100% 100% !important;
}
</style>

四、import.meta.glob

<template><div ref="titleImage" :class="['a-bg', systemTitle.value]"></div>
</template><script setup>
import { ref, watch, onMounted } from "vue";// 随机赋值
const possibleValues = ['one', 'two', 'three'];
const randomIndex = Math.floor(Math.random() * possibleValues.length);
const systemTitle = ref(possibleValues[randomIndex]);const titleImage = ref(null);// 动态导入图片
const images = import.meta.glob('@/assets/image/*.jpg');// 对象映射
const classMapping = {one: 'cat1',two: 'cat2',three: 'cat3'
};const updateTitleImageClass = async () => {if (titleImage.value) {console.log('Before:', titleImage.value.className); // 打印当前类名// 清除所有可能的类名for (const key in classMapping) {titleImage.value.classList.remove(key);}// 添加新的类名titleImage.value.classList.add(systemTitle.value);console.log('After:', titleImage.value.className); // 打印替换后的类名// 动态加载图片const imagePath = `/src/assets/image/${classMapping[systemTitle.value]}.jpg`; // 根据系统标题获取图片路径const imageModule = await images[imagePath]();  // 使用动态导入const imageUrl = imageModule.default; // 获取图片路径titleImage.value.style.backgroundImage = `url(${imageUrl})`; // 设置背景图片}
};onMounted(() => {console.log(systemTitle.value, 'systemTitle');updateTitleImageClass();
});// 监听 systemTitle 的变化
watch(systemTitle, updateTitleImageClass);
</script><style lang="scss" scoped>
#app {width: 100%;height: 100vh;
}.a-bg {width: 200px;height: 200px;background: linear-gradient(45deg, #fff, red);position: relative;background-size: 100% 100%;
}.one::before,
.two::before,
.three::before {content: '';display: block;position: absolute;top: 0;left: 0;width: 100%;height: 100%;
}
</style>

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

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

相关文章

精确的单向延迟测量:使用普通硬件和软件

论文标题&#xff1a;Precise One-way Delay Measurement with Common Hardware and Software&#xff08;精确的单向延迟测量&#xff1a;使用普通硬件和软件&#xff09; 作者信息&#xff1a;Maciej Muehleisen 和 Mazen Abdel Latif&#xff0c;来自Ericsson Research Eri…

基于Java+Swing+Mysql的网络聊天室

博主介绍&#xff1a; 大家好&#xff0c;本人精通Java、Python、C#、C、C编程语言&#xff0c;同时也熟练掌握微信小程序、Php和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我有丰富的成品Java、Python、C#毕设项目经验&#xff0c;能够为学生提供各类…

Linux-音频应用编程

ALPHA I.MX6U 开发板支持音频&#xff0c;板上搭载了音频编解码芯片 WM8960&#xff0c;支持播放以及录音功能&#xff01;本章我们来学习 Linux 下的音频应用编程&#xff0c;音频应用编程相比于前面几个章节所介绍的内容、其难度有所上升&#xff0c;但是笔者仅向大家介绍 Li…

vue3 项目搭建-9-通过 router 在跳转页面时传参

第一步&#xff0c;在跳转链接处挂载方法&#xff0c;将要传输的数据传入&#xff1a; <a href"#" click.prevent"goToArticle(obj.id)" class"click"><h1>{{obj.title}}</h1><p>作者&#xff1a;{{obj.author}}</p&…

【RBF SBN READ】hadoop社区基于RBF的SBN READ请求流转

读写分离功能的背景及架构 当前联邦生产集群的各个子集群只有Active NameNode在工作,当读写任务变得繁忙的时候,只有一个Active负责处理的话,此时集群的响应和处理能力业务侧感知会明显下降,为此,我们将引入Observer架构,实现读写功能的分离,使得Active只负责写请求,而…

利用R包QstFstComp包进行Qst-Fst分析

1.Qst-Fst分析 安装和加载QstFstComp包 首先&#xff0c;你需要安装devtools包&#xff0c;如果尚未安装&#xff0c;可以使用以下命令安装&#xff1a; install.packages("devtools") 2. 然后&#xff0c;使用devtools安装QstFstComp包&#xff1a;R library(de…

视频自学笔记

一、视频技术基本框架 二、视频信号分类 2.1信号形式 2.1.1模拟视频 模拟视频是指由连续的模拟信号组成的视频图像&#xff0c;以前所接触的电影、电视都是模拟信号&#xff0c;之所以将它们称为模拟信号&#xff0c;是因为它们模拟了表示声音、图像信息的物理量。摄像机是获…

操作系统——大容量存储结构

笔记内容及图片整理自XJTUSE “操作系统” 课程ppt&#xff0c;仅供学习交流使用&#xff0c;谢谢。 大容量存储结构概述 磁盘 磁盘为现代计算机系统提供大量外存。每个盘片为平的圆状&#xff08;类似CD&#xff09;&#xff0c;普通盘片直径为4.5~9.0厘米。盘片的两面都涂着…

Python 正则表达式常用特殊字符及其含义

以下是 Python 正则表达式常用特殊字符及其含义 的全面整理&#xff0c;涵盖了常见和重要的正则符号&#xff0c;以及它们的示例&#xff0c;适合用来写博客或学习使用&#xff1a; Python 正则表达式常用特殊字符及其含义 1. . (点号) 含义&#xff1a;匹配除换行符 \n 以外…

子类调用父类同名方法和属性

1、全量代码 class Master:def __init__(self):self.kongfu [古法煎饼果子配方]print(fMaster_self:{self})def make_cake(self):print(f运用{self.kongfu}制作煎饼果子)class School():def __init__(self):self.kongfu [学校煎饼果子配方]print(fSchool_self:{self})def m…

Java Serializable 序列化

Java的Serializable接口是Java序列化机制的核心&#xff0c;它允许一个对象的状态被转换为字节流&#xff0c;从而可以方便地进行存储或传输。 序列化后的对象可以被写到数据库、存储到文件系统&#xff0c;或者通过网络传输。 要在 Java 中使一个类可序列化&#xff0c;你需要…

CSS一些小点 —— 12.7

1. box-sizing: border-box box-sizing 属性&#xff0c;默认值为 content-box box-sizing: border-box 使padding和border的值不会再影响元素的宽高&#xff1b;padding和border的值算在指定宽高的内部&#xff08;但是外边距依然算做外部&#xff09; 2. overflow: hidden …

【GESP】C++一级练习 luogu-P1425, 小鱼的游泳时间

GESP一级综合练习&#xff0c;主要涉及时间计算&#xff0c;难度★☆☆☆☆。 题目题解详见&#xff1a;https://www.coderli.com/gesp-1-luogu-p1425/ 【GESP】C一级练习 luogu-P1425, 小鱼的游泳时间 | OneCoderGESP一级综合练习&#xff0c;主要涉及时间计算&#xff0c;难…

【网络协议栈】数据链路层(内附手画分析图 简单易懂)以太网、MAC地址、局域网、交换机、MTU、ARP协议

每日激励&#xff1a;【无限进步】“梦想可以大&#xff0c;但第一步总是小” 绪论​&#xff1a; 本章将开始到达TCP/IP协议中的最后一层数据链路层&#xff0c;本章将会写到我们日常中常见的局域网以及认识数据链路层中非常重要的协议ARP协议&#xff0c;后续还将进行更新网络…

docker怎么commit tag push?

在 Docker 中&#xff0c;commit、tag 和 push 是用于创建和推送自定义镜像到仓库的三个不同步骤。以下是每个命令的详细说明和使用方法&#xff1a; ### 1. docker commit 当你对一个运行中的容器做了修改&#xff0c;并希望将这些修改保存为一个新的镜像时&#xff0c;可以使…

Java多线程与线程池技术详解(四)

接受失败&#xff1a;“失败是什么&#xff1f;没有什么&#xff0c;只是更走近成功一步&#xff1b;成功是什么&#xff1f;就是走过了所有通向失败的路&#xff0c;只剩下一条路&#xff0c;那就是成功的路。”这句话很好地诠释了如何看待失败的问题&#xff0c;即每一次跌倒…

二叉树的深搜(不定期更新。。。。。)

二叉树的深搜 验证二叉搜索树 给你一个二叉树的根节点 root &#xff0c;判断其是否是一个有效的二叉搜索树。 有效 二叉搜索树定义如下&#xff1a; 节点的左 子树 只包含 小于 当前节点的数。 节点的右子树只包含 大于 当前节点的数。 所有左子树和右子树自身必须也是二叉…

51c嵌入式~单片机合集3

我自己的原文哦~ https://blog.51cto.com/whaosoft/12581900 一、STM32代码远程升级之IAP编程 IAP是什么 有时项目上需要远程升级单片机程序&#xff0c;此时需要接触到IAP编程。 IAP即为In Application Programming&#xff0c;解释为在应用中编程&#xff0c;用户自己的程…

使用setsockopt函数SO_BINDTODEVICE异常,Protocol not available

前言 最近在使用OLT的DHCP Server的时候发现一些异常现象&#xff0c;就是ONU发的一个vlan的discover包其他不同vlan的DHCP地址池也会收到&#xff0c;导致其他服务器也发了offer包&#xff0c;ONU同时会有多个ip地址。一开始是没有使用SO_BINDTODEVICE&#xff0c;后面查到使…

02 conda常用指令

目录 命令快速查找命令详细解释列出当前conda中存在的解释器环境使用指定的解释器环境创建虚拟环境激活自己创建的虚拟环境虚拟环境删除切换回主环境找到你计算机中安装的miniconda3的跟目录找到虚拟环境的目录选择需要删除的虚拟环境文件夹确认环境是否删除 补充删除虚拟环境指…