admin管理员组文章数量:1131553
I want to concatenate Vue.js variable with image URL.
What I computed:
imgPreUrl : function() {
if (androidBuild) return "android_asset/www/";
else return "";
}
If I build for android:
<img src="/android_asset/www/img/logo.png">
Else
<img src="img/logo.png">
How can I concatenate the computed variable with the URL?
I tried it:
<img src="{{imgPreUrl}}img/logo.png">
I want to concatenate Vue.js variable with image URL.
What I computed:
imgPreUrl : function() {
if (androidBuild) return "android_asset/www/";
else return "";
}
If I build for android:
<img src="/android_asset/www/img/logo.png">
Else
<img src="img/logo.png">
How can I concatenate the computed variable with the URL?
I tried it:
<img src="{{imgPreUrl}}img/logo.png">
Share
Improve this question
edited Oct 27, 2018 at 0:16
nyedidikeke
7,5988 gold badges47 silver badges61 bronze badges
asked Oct 26, 2016 at 6:35
ketomketom
2,4444 gold badges19 silver badges25 bronze badges
0
7 Answers
Reset to default 301You can't use curlies (moustache tags) in attributes. Use the following to concat data:
<img v-bind:src="imgPreUrl + 'img/logo.png'">
Or the short version:
<img :src="imgPreUrl + 'img/logo.png'">
Read more on dynamic attributes in the Vue docs.
In another case I'm able to use template literal ES6 with backticks, so for yours could be set as:
<img v-bind:src="`${imgPreUrl()}img/logo.png`">
just try
<img :src="require(`${imgPreUrl}img/logo.png`)">
Following both method is valid.
Method 1
Concatenate with +
sign and wrap string with single/double quotation.
<img :src="imgPreUrl() + 'img/logo.png'">
Method 2
Wrap with backtick `
and wrap variables with ${variable}
. As imgPreUrl
is a method so,
<img :src="`${imgPreUrl()}img/logo.png`">
if you handel this from dataBase try :
<img :src="baseUrl + 'path/path' + obj.key +'.png'">
If it helps, I am using the following to get a gravatar image:
<img
:src="`https://www.gravatar.com/avatar/${this.gravatarHash(email)}?s=${size}&d=${this.defaultAvatar(email)}`"
class="rounded-circle"
:width="size"
/>
For me, it said Module did not found and not worked. Finally, I found this solution and worked.
<img v-bind:src="require('@' + baseUrl + 'path/path' + obj.key +'.png')"/>
Needed to add '@' at the beginning of the local path.
本文标签: javascriptVuejs img src concatenate variable and textStack Overflow
版权声明:本文标题:javascript - Vue.js img src concatenate variable and text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736769595a1952010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论