javascript – Vue.js img src concatenate variable and text-ThrowExceptions

Exception or error:

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">
How to solve:

You 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`)">

###

if you handel this from dataBase try :

<img :src="baseUrl + 'path/path' + obj.key +'.png'">

Leave a Reply

Your email address will not be published. Required fields are marked *