因为小程序有打包体积的限制,所以大部分图片我都放在服务器上,后端搭建了一个容器给我存放图片用,那么我在用到图片的时候src设置正确的线上地址就能访问了,为了方便线上线下图片地址的切换,在main.js中定义全局的函数来统一管理这个地址。
Vue.prototype.staticDir = '这里放存图片的容器地址';
Vue.prototype.getStaticFilePath = function (url) {
return Vue.prototype.staticDir + url;//这里的url就是存放在这个容器地址的图片名称包括后缀,具体根据自己情况改
};
接下来在页面中使用,比如轮播图
//这段在标签中
<u-swiper previousMargin="20" bgColor="#5298ff00" nextMargin="20" height="160" :list="list5" circular="true" @change="e => current = e.current">
</u-swiper>
//这段在data中定义的数据,因为我uniapp用的vue2写法,不同的自行更改
list5: [
this.getStaticFilePath('/homeBanner1.png'),
this.getStaticFilePath('/homeBanner2.png'),
this.getStaticFilePath('/homeBanner3.png'),
],
或者直接用在图片标签中
<image lazy-load="true" class="titleIcon" :src="getStaticFilePath('/xunIcon.png')"></image>