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>

源码下载

相关推荐
胡萝卜术2 小时前
从API调用到手写LRU:力扣146「LRU缓存」的哈希表+双向链表终极进化之路
前端·javascript·面试
科技道人2 小时前
记录 默认置灰/禁用 app ‘Search Engine Selector‘ 的disable按钮
开发语言·前端·javascript
天疆说2 小时前
Zotero Connector 在 Edge 里找不到桌面 Zotero(Linux / Zotero 9.0.6 / Edge 150)
linux·前端·edge
李伟_Li慢慢4 小时前
02-从硬件说起WebGL
前端·three.js
kyriewen5 小时前
看了微软几万人用AI编程的数据——效率涨24%的代价没人提
前端·ai编程·claude
2601_963771375 小时前
How to Scale Your WordPress Store Traffic in 2026
前端·php·plugin
亿元程序员5 小时前
老板说我的3D箭头游戏用来做试玩太普通了,没人想玩,让我变点花样...
前端
李伟_Li慢慢5 小时前
01-threejs架构原理-课程简介
前端·three.js
_瑞7 小时前
深入理解 iOS 渲染原理
前端·ios
IT_陈寒7 小时前
SpringBoot自动配置失灵?你可能忘了这个关键注解
前端·人工智能·后端