VUEJS组件中的图像未加载
我得到了一个父组件,将数据对象发送到这样的孩子组件:
< child object-data =“ url:url:url onere',title:'title''><</child>
然后在我的孩子组件上,我通过执行:
<script>
export default {
props: [
'objectData'
]
}
</script>
现在,出于某种原因,我可以使用标题而没有任何问题{objectdata.title}}}
并显示出来。
但是,当涉及IMG标签内的URL时,它不会呈现图像。
我尝试执行以下操作:
&lt; img:src =“ objectdata.url”/&gt;
&lt; ----不渲染
&lt; img v-bind:src =“ objectdata.url “/&gt;
&lt; ---不渲染
&lt; img v-bind:src =“ require(objectdata.url)”/&gt;
&lt; - 引发警告错误因为这不是一条路径,而是一个对象。
&lt; img v-bind:src =“ {objectdata.url}”/&gt;
&lt; -----抛出错误
&lt; img v-bind:src ='{{objectdata 。
如果我在没有对象的情况下写下URL,则可以工作。
&lt; img v-bind:src =“ src/assets/images/icon.png”/&gt;
,
但我希望我的URL来自父组件。
关于如何解决这个问题的想法?我在WebPack文件上添加了URL-LOADER:
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader'
}
我还尝试从计算/方法fuction返回objectdata.url
fuction:
computed: {
getImageUrl: function() {
return objectData.url;
}
}
然后将其像:src =“ getimageUrl”
或:src =“ {{getimageurl}}”
,我也不幸运。
I got a parent component which sends a data object to the children component like this:
<child object-data=" url: 'url here', title: 'Title'"></child>
Then on my children component, I get this object data by doing:
<script>
export default {
props: [
'objectData'
]
}
</script>
Now, for some reason, I can use title without any problem like this {{ objectData.title }}
and shows up.
But when it comes to the URL inside an img tag it's not rendering the image.
I attempted doing the following:
<img :src="objectData.url"/>
<--- not rendering
<img v-bind:src="objectData.url"/>
<--- not rendering
<img v-bind:src="require(objectData.url)"/>
<-- throws a warning error because it's not a path but an object I guess.
<img v-bind:src="{objectData.url}"/>
<--- throws an error
<img v-bind:src="{{objectData.url}}"/>
<--- throws an error
When I check on the dom element, it doesn't even contain a src attribute afterwards.
If I write down the URL without an object, it works.
<img v-bind:src="src/assets/images/icon.png"/>
But I want my URL to come from a parent component.
Any ideas on how to solve this? I added the url-loader on my webpack file:
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader'
}
I also attempted returning objectData.url
from a computed/methods fuction:
computed: {
getImageUrl: function() {
return objectData.url;
}
}
And then use it like :src=“getImageUrl”
or :src=“{{getImageUrl}}”
and I wasn’t lucky either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同一问题,并通过使用
require
函数修复了它:在parent component
app.vue
中:在儿童组件中,我通过
posts post 并绑定图像
src
如下:因此,在您的情况下,您应该有类似的内容:
更新:
我的项目树:
I faced the same issue and i fixed it by using
require
function :in the parent component
App.vue
:in the child component i loop through the
posts
and bind the imagesrc
as follows :So in your case you should have something like :
Update :
my project tree :
两种替代方法:
2019年11月的文档相关的文件:
https://github.com/vuejs/vuejs/vue-loader/loader/sissues/1612
Two alternatives:
Documentation relevant effective Nov 2019:
https://github.com/vuejs/vue-loader/issues/1612