首页 前端知识 vue3中动态引入静态资源

vue3中动态引入静态资源

2024-10-15 23:10:46 前端知识 前端哥 356 163 我要收藏

vite:在vue3中动态引入静态资源图片需要使用 new URL(url, import.meta.url)方法

注意:如果作为背景图片,要使用相对路径,不然打包时无法解析

参考: 静态资源处理 | Vite 官方中文文档

const imgUrl = [
  new URL('../src/assets/images/home/home_card_1.png', import.meta.url).href,
  new URL('../src/assets/images/home/home_card_2.png', import.meta.url).href,
  new URL('../src/assets/images/home/home_card_3.png', import.meta.url).href,
  new URL('../src/assets/images/home/home_card_4.png', import.meta.url).href,
  new URL('../src/assets/images/home/home_card_5.png', import.meta.url).href,
  new URL('../src/assets/images/home/home_card_6.png', import.meta.url).href,
]

将图片作为背景图片使用


    <div v-for="(item,index) in growCardList" :key="item.title">
      <div 
        :style="{'backgroundImage':`url(${imgUrl[index]}`}"
        }">
      </div>
    </div>

webpack:通过require动态引入

<img :src="require('@/assets/images/home/home_bg.png')" />

转载请注明出处或者链接地址:https://www.qianduange.cn//article/19018.html
标签
评论
发布的文章

3.1 requests与JSON基础

2024-10-15 23:10:08

React 安装(NPM)

2024-10-15 23:10:15

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!