图片url处理(带http和不带http)方法

后端返回的接口数据,图片路径,有的是相对路径,有的是带http的路径

得处理一下,如果是相对路径就拼上服务器的地址(xxx),如果是带http的路径就正常显示

方法:

javascript 复制代码
imageUrl(url){
  let str = RegExp('http');
  let newUrl;
  str.test(url) ? newUrl = url : newUrl = xxx + url;
  // xxx就是需要拼接的地址
  console.log(newUrl);
  return newUrl
}

测试了一下,一个是相对路径(/static/img/logo.png),一个是带http的路径

打印出来是没问题的

javascript 复制代码
var imgSrc = '/static/img/logo.png'  
// www.xxx.com/static/img/logo.png

var imgSrc = 'https://img-home.csdnimg.cn/images/20201124032511.png'
// https://img-home.csdnimg.cn/images/20201124032511.png

// 调用
this.imageUrl(imgSrc)
相关推荐
GISer_Jing8 小时前
Three.js着色器编译机制深度解析
javascript·webgl·着色器
丷丩8 小时前
MapLibre GL JS第22课:查看本地GeoJSON
前端·javascript·map·mapbox·maplibre gl js
AI玫瑰助手9 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车9 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋9 小时前
C++14特性
开发语言·c++·c++14特性
JAVA社区10 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展
弥树子10 小时前
踩坑记录:服务器内网调用接口,真实请求URL与官方公开URL不一致问题排查
开发语言·php
z落落11 小时前
C# ToCharArray + foreach遍历 + String与StringBuilder
开发语言·c#
学代码的真由酱11 小时前
Java多用户一对一网页聊天室-测试报告
java·开发语言·功能测试·测试
人道领域11 小时前
【LeetCode刷题日记】669.修剪二叉搜索树
开发语言·python·算法