【发布时间】:2019-09-28 18:44:51
【问题描述】:
我正在尝试使用 props 将图像 url 加载到组件中,但似乎 require 不能接受任何变量。但是,如果我给 require 一个纯文本作为参数,它可以工作
这个给出错误
在 webpackEmptyContext 中找不到模块 '../assets/logo.png' (评估在 ./src/component
<template>
<div>
{{imagelink}}
<div style="width: 150px; height: 150px; background-color: red">
<img :src="imglink" alt="" height="150px" width="150px">
</div>
</div>
</template>
<script>
export default {
name: "ImageTest",
props:{
imagelink: String,
},
computed: {
imglink: function () {
// this.imagelink
const modulepatha = '../assets/logo.png'
return require(modulepatha)
}
}
}</script>
<style scoped>
</style>
这个有效:
<template>
<div>
{{imagelink}}
<div style="width: 150px; height: 150px; background-color: red">
<img :src="imglink" alt="" height="150px" width="150px">
</div>
</div>
</template>
<script>
export default {
name: "ImageTest",
props:{
imagelink: String,
},
computed: {
imglink: function () {
// this.imagelink
const modulepatha = '../assets/logo.png'
return require('../assets/logo.png') //changed this
}
}
}</script>
<style scoped>
</style>
请注意,我只将 require 中的值更改为纯文本
【问题讨论】:
标签: vue.js webpack vue-component