文件结构(Webpack)
src
-assets
--img
---myimage.png
-vue
--page
---parent.vue
---component
----child.vue
parent.vue :在这里,我尝试将
src
相对于此组件和相对于实际加载图像的子组件进行处理。
/** This data is passed to the child component in a loop */
data(){
return{
items: [
{ id: 1, src: '../../../assets/img/myImage.png'},
more items...
]
}
}
child.vue :控制台显示以下错误:在挂载钩子中出现错误:"Error: Cannot find module '../../../assets/img/myImage.png'"
props: {
item: {
type: Object,
required: true
}
},
mounted() {
let element = this.$refs['myelement'];
let img = new Image();
img.onload = function(){
element.insertBefore(this, element.firstChild);
}
img.src = require(this.item.src);
},
我已经尝试了所有可能的src组合,甚至将图像文件复制到组件目录中,并引用它,例如
myImage.png
和./myImage.png
,但是出现了相同的错误。发生了什么?
http://localhost:8080/src/assets/img/myImage.png
。 - StudentX