在JavaScript中引用服务器地址的网络图片,可以使用如下方式:

image: `${this.baseURL}/api/file/Image/systemfile/icon-1.png`

或者:

image: this.baseURL + '/api/file/Image/systemfile/icon-1.png'

注意在第二种方式中要将引号放在整个URL的外面。同时,如果在返回的数组中使用第二种方式,可以直接使用this关键字引用baseURL。例如:

workStateList: [{
	// image: '../../static/teamLeader/icon-1.png',
	image: this.baseURL + '/api/file/Image/systemfile/icon-1.png',
	id: 3,
	value: 1,
	lable: '工作人员'
},
{
	// image: '../../static/teamLeader/icon-2.png',
	image: this.baseURL + '/api/file/Image/systemfile/icon-2.png',
	id: 2,
	value: 2,
	lable: '待工人员'
},
{
	// image: '../../static/teamLeader/icon-3.png',
	image: this.baseURL + '/api/file/Image/systemfile/icon-2.png',
	value: 3,
	id: 4,
	lable: '请假人员'
}
],

tabColorList: [{
	color: '#67DF67',
	background: '#F6FFF6'
},
{
	color: '#6BCDFF',
	background: '#F4FBFF'
},
{
	color: '#8F8FF8',
	background: '#F7F7FF'
}
]

最佳实践

建议使用模板字符串的方式来引用网络图片,这样可以提高代码的可读性和可维护性。

JavaScript 引用服务器地址网络图片的正确方法

原文地址: https://www.cveoy.top/t/topic/noF3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录