Vue3 动态加载静态资源

文章目录

Vue3 动态加载静态资源

效果图

代码实现

方式一: new URL()+computed

vue 复制代码
<script setup>
import {computed, ref} from "vue";

const seasons = ref(["spring", "summer", "autumn", "winter"]);
const currentSeason = ref("spring");

const imageUrl = computed(() => {
  return new URL(`../assets/images/${currentSeason.value}.png`, import.meta.url).href;
});

function handleClick(season) {
  currentSeason.value = season;
}
</script>

<template>
  <div class="container" :style="{'background-image':`url(${imageUrl})`}">
    <div class="season-tabs">
      <button v-for="season in seasons" :key="season"
              class="season-btn"
              :class="{'season-btn-active': currentSeason === season}"
              @click="handleClick(season)"
      >
        {{ season }}
      </button>
    </div>
  </div>
</template>

<style scoped>
.container {
  width: 100vw;
  height: 100vh;
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
}

.season-tabs {
  display: flex;
  gap: 12px;
}

.season-btn {
  padding: 10px 24px;
  border: none;
  border-radius: 4px;
  background-color: #fff;
  font-size: 16px;
  cursor: pointer;
}

.season-btn-active {
  background-color: #409eff;
  color: #fff;
}
</style>

方式二: watch+ref

vue 复制代码
<script setup>
import {computed, ref, watch} from "vue";

const seasons = ref(["spring", "summer", "autumn", "winter"]);
const currentSeason = ref("spring");

const imageUrl = ref("");
watch(currentSeason, (newSeason) => {
  import(`../assets/images/${newSeason}.png`).then(m => {
    imageUrl.value = m.default;
  }).catch(e => console.error(e));
}, {immediate: true});

function handleClick(season) {
  currentSeason.value = season;
}
</script>

<template>
  <div class="container" :style="{'background-image':`url(${imageUrl})`}">
    <div class="season-tabs">
      <button v-for="season in seasons" :key="season"
              class="season-btn"
              :class="{'season-btn-active': currentSeason === season}"
              @click="handleClick(season)"
      >
        {{ season }}
      </button>
    </div>
  </div>
</template>

<style scoped>
.container {
  width: 100vw;
  height: 100vh;
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
}

.season-tabs {
  display: flex;
  gap: 12px;
}

.season-btn {
  padding: 10px 24px;
  border: none;
  border-radius: 4px;
  background-color: #fff;
  font-size: 16px;
  cursor: pointer;
}

.season-btn-active {
  background-color: #409eff;
  color: #fff;
}
</style>

方式三:对象管理

vue 复制代码
<script setup>
import {computed, ref, watch} from "vue";

import spring from "@/assets/images/spring.png";
import summer from "@/assets/images/summer.png";
import autumn from "@/assets/images/autumn.png";
import winter from "@/assets/images/winter.png";
const seasons = ref(["spring", "summer", "autumn", "winter"]);
const currentSeason = ref("spring");

const seasonImages = {
  spring,
  summer,
  autumn,
  winter
};

const imageUrl = computed(() => seasonImages[currentSeason.value]);

function handleClick(season) {
  currentSeason.value = season;
}
</script>

<template>
  <div class="container" :style="{'background-image':`url(${imageUrl})`}">
    <div class="season-tabs">
      <button v-for="season in seasons" :key="season"
              class="season-btn"
              :class="{'season-btn-active': currentSeason === season}"
              @click="handleClick(season)"
      >
        {{ season }}
      </button>
    </div>
  </div>
</template>

<style scoped>
.container {
  width: 100vw;
  height: 100vh;
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
}

.season-tabs {
  display: flex;
  gap: 12px;
}

.season-btn {
  padding: 10px 24px;
  border: none;
  border-radius: 4px;
  background-color: #fff;
  font-size: 16px;
  cursor: pointer;
}

.season-btn-active {
  background-color: #409eff;
  color: #fff;
}
</style>

源码下载

相关推荐
子兮曰1 小时前
whisper.cpp 深度解析:从边缘设备到实时语音识别
前端·c++·后端
子兮曰1 小时前
Ruflo 深度解析:49K Stars 的 AI Agent 编排平台 — 给 Claude Code 装上分布式神经系统
前端·后端·ai编程
小皮咖1 小时前
发给那个让你加班的同事
前端
克里斯蒂亚诺更新1 小时前
ruoyi切换新版本初始化需要修改的地方
前端·javascript·vue.js
可视之道1 小时前
基于Meta2d.js的电力系统组态平台实战开发
前端
小村儿1 小时前
(译文)重温:Karpathy 的 4 条 CLAUDE.md 规则将 Claude 错误率从 41% 降至 11%——历经 30 个代码库后,我又加了 8 条
前端·后端·ai编程
前端那点事1 小时前
Vite+Vue3环境判断终极解法!区分开发/生产环境,告别环境报错
前端·vue.js
源码集结号1 小时前
基于 Spring Boot + JPA + MySQL的上门家政系统代码示例
java·前端·后端
爱喝铁观音的谷力景辉1 小时前
web端实现音频波形分析以及音频截取
前端