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 分钟前
实现 GEO 监控:从多引擎探测到优化闭环
前端·后端
甲维斯2 分钟前
GLM5.2+ZCode复刻坦克大战,自测50万帧!
前端·ai编程·游戏开发
Csvn1 小时前
useRef 的 5 个冷门但救命的高级用法
前端
小小小小宇1 小时前
Harness Engineering 与 AI 联动
前端
mqcode1 小时前
你项目里的 axios,封对了吗?从裸用到生产级的四步进化
vue.js·axios
鱼人1 小时前
HTML5 页面性能优化大全
前端
ping某1 小时前
专栏-null 和 undefined 到底是什么?
前端·javascript·后端
用户900463370401 小时前
5MB vs 4KB vs 无限大:浏览器存储谁更强?
前端
小小小小宇1 小时前
Harness Engineering 全解析与应用
前端
牧艺2 小时前
cos-design v3.0:从 15 个 Demo 到 49 个组件的视觉特效库
前端·视觉设计