需求,给地图添加marker标记点,使用本地的小图标。
1. 首先,在vue3中无法使用require。
2. 直接在添加标记点的地方,使用以下四种src引入方式,都可以在h5中显示出图片,但是在APP中就无法加载了。
image: new Icon({
// src: '../static/icons/tip_red.png',
// src: '../../static/icons/tip_red.png',
// src: '/src/static/icons/tip_red.png',
src: '/static/icons/tip_red.png',
anchor: [0.5, 1],
scale: 0.18,
}),
3. 使用import的方式引入,在hbuilderX中编译就会报错,无论使用怎样的路径,代码举例
import tipRed from '/src/static/icons/tip_red.png'
import tipRed from '../static/icons/tip_red.png'
import tipRed from '../../static/icons/tip_red.png'
import tipRed from '/static/icons/tip_red.png'
import tipRed from 'static/icons/tip_red.png'
import tipRed from '@/static/icons/tip_red.png'
image: new Icon({
src: tipRed,
anchor: [0.5, 1],
scale: 0.18,
}),
4. 那就采用赋值的方式引入图片试试
const tipRed = '../../static/icons/tip_red.png'
const tipRed = '../static/icons/tip_red.png'
const tipRed = '/src/static/icons/tip_red.png'
const tipRed = '@/static/icons/tip_red.png'
const tipRed = '/static/icons/tip_red.png'
image: new Icon({
src: tipRed,
anchor: [0.5, 1],
scale: 0.18,
}),
**以上方式在hbuilderX编译的时候并不会报错,但是在APP上仍旧无法显示图片**
我的static图片目录,是在src目录下的,而src是在根目录的。
经过验证,只有如下引用方式,可以在APP正常显示renderjs本地引用的图片
const tipRed = 'static/icons/tip_red.png'
image: new Icon({
src: tipRed,
anchor: [0.5, 1],
scale: 0.18,
}),