uniapp判斷圖片有沒有http前綴,沒有的話追加自己域名前綴,適用于接口數據既有外鏈也有本地鏈接的情況
/**
* 文件或圖片轉換http或者添加域名
*/
imgHttp(content) {
if (content.includes("http:")) {
// console.log("包含 http:");
} else {
// console.log("不包含 http:");
content = 'http://xxx.com' + content;//追加自己域名前綴,例如或獲取.env等全局配置的域名
}
return content;
},
使用方式如(該方法放在全局方法common.js):

這樣圖片等文件,有http前綴的就原樣返回,沒有前綴的則加前綴

每天進步一點點

浙公網安備 33010602011771號