简体   繁体   English

Vue.js img src 连接变量和文本

[英]Vue.js img src concatenate variable and text

I want to concatenate Vue.js variable with image URL.我想将 Vue.js 变量与图像 URL 连接起来。

What I computed:我计算的是:

imgPreUrl : function() {
    if (androidBuild) return "android_asset/www/";
    else return "";
}

If I build for android:如果我为 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?如何将计算变量与 URL 连接起来?

I tried it:我尝试过这个:

<img src="{{imgPreUrl}}img/logo.png">

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 .Vue 文档中阅读有关动态属性的更多信息。

在另一种情况下,我可以使用带有反引号的模板文字 ES6,因此您可以将其设置为:

<img v-bind:src="`${imgPreUrl()}img/logo.png`">

试试吧

 <img :src="require(`${imgPreUrl}img/logo.png`)">

如果您从数据库中处理此问题,请尝试:

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

Doesn't work: 不起作用:

<div :class="['some-class', someClassNameProp, { `some-class--${specificModifierProp}`: showSomethingSpecificProp }]" >

Better 更好

<div :class="['some-class', someClassNameProp, showSomethingSpecificProp ? specificModifierComputed : '']" >

...

computed: {
  specificModifierComputed() {
    return `some-class--${specificModifierProp}`;
  }
},

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.需要在本地路径的开头添加“@”。

If it helps, I am using the following to get a gravatar image:如果有帮助,我将使用以下内容来获取 Gravatar 图像:

<img
        :src="`https://www.gravatar.com/avatar/${this.gravatarHash(email)}?s=${size}&d=${this.defaultAvatar(email)}`"
        class="rounded-circle"
        :width="size"
    />

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} .用反引号`包裹并用${variable}包裹变量。 As imgPreUrl is a method so,由于imgPreUrl是一种方法,因此,

<img :src="`${imgPreUrl()}img/logo.png`">

Gracias a todos por su aporte a mi me funciono esto: Gracias a todos por su aporte a mi me funcion esto:

Donde "user.Imagen" es la variable que definí de acuerdo al usuario para que el avatar pueda cambiar en cada inicio de sesión.不要将“user.Imagen”作为变量来定义 acuerdo al usuario para el avatar pueda cambiar en cada inicio de sesión。

Incrustacion utilizada en Google AppScript在 Google AppScript 中嵌入应用程序

Saludos Cordiales. Saludos Cordiales。

 <v-list-item class="px-2"> <v-list-item-avatar> <img v-bind:src="user.Imagen"></v-img> </v-list-item-avatar> </v-list-item>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM