javascript - VueJs - 将字符串放入 img src 属性中

标签 javascript vue.js

我需要为 vue 组件获取一段数据(具体来说,工作部门的名称),将其转换为小写并将其附加到图像 src 中以渲染正确的图像。因为这些图像只是部门的名称。

目前看起来像这样:

<ul class="jobs-container">
  <li
      v-for="job in filteredJobs"
      :key="job.absolute_url"
      class="u-margin-bottom-small">
    <a :href="job.absolute_url" target="_blank">
      <img src="/wp-content/themes/theme/dist/images/icons/careers/engineering.svg" alt="Engineering" />
      <div>
        <h3 v-html="job.departments[0].name" />
        <span class="position" v-html="job.title" />
      </div>
      <span class="location" v-html="job.offices[0].location" />
    </a>
  </li>
</ul>

我需要做这样的事情:

<ul class="jobs-container">
  <li
      v-for="job in filteredJobs"
      :key="job.absolute_url"
      class="u-margin-bottom-small">
    <a :href="job.absolute_url" target="_blank">

      // insert name of job department
      <img src="/wp-content/themes/theme/dist/images/icons/careers/{{ job.departments[0].name.toLowerCase() }}.svg" alt="Engineering" />

      <div>
        <h3 v-html="job.departments[0].name" />
        <span class="position" v-html="job.title" />
      </div>
      <span class="location" v-html="job.offices[0].location" />
    </a>
  </li>
</ul>

这不起作用并抛出:

ERROR Failed to compile with 1 errors 4:02:34 PM

error in ./wp-content/themes/theme/assets/js/Careers. vue?vue&type=template&id=bf690b58&

Module Error (from ./node_modules/vue-loader/lib/loader s/templateLoader.js): (Emitted value instead of an instance of Error)

Errors compiling template:

src="/wp-content/themes/theme/dist/images/icons/caree rs/{{ job.departments[0].name }}.svg": Interpolation in side attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of <div id="{{ val }}">, use <div :id="val">.

30 | class="u-margin-bottom-small"> 31 | 32 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 33 | 34 |

那么我就走了:

<img :src="/wp-content/themes/theme/dist/images/icons/careers/{{ job.departments[0].name.toLowerCase() }}.svg" alt="Engineering" />

错误:

ERROR Failed to compile with 1 errors 4:06:01 PM

error in ./wp-content/themes/theme/assets/js/Careers. vue?vue&type=template&id=16c0d4b0&

Module Error (from ./node_modules/vue-loader/lib/loader s/templateLoader.js): (Emitted value instead of an instance of Error)

Errors compiling template:

invalid expression: Invalid regular expression flags in

/wp-content/themes/theme/dist/images/icons/careers/{{ job.departments[0].name }}.svg

Raw expression: :src="/wp-content/themes/theme/dist/images/icons/careers/{{ job.departments[0].name }}.svg"

30 | class="u-margin-bottom-small"> 31 | 32 | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 33 | 34 |

@ ./wp-content/themes/theme/assets/js/Careers.vue?vue&type=template&id=16c0d4b0& 1:0-223 1:0-223 @ ./wp-content/themes/theme/assets/js/Careers.vue @ ./wp-content/themes/theme/assets/js/careers.js @ ./wp-content/themes/theme/assets/js/main.js @ multi ./wp-content/themes/theme/assets/js/main.js ./wp-content/themes/theme/assets/sass/base.scss

在 Vue 的上下文中我如何获取 job.departments[0].name.toLowerCase()并注入(inject) <img src="/wp-content/themes/theme/dist/images/icons/careers/(INJECT IT HERE).svg" alt="Engineering" /> 的最后一部分

最佳答案

使用模板文字:

  <img :src="`/url/${name}.svg`" alt="Engineering" />

关于javascript - VueJs - 将字符串放入 img src 属性中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58614654/

相关文章:

javascript - 谷歌地图未捕获类型错误 : Cannot read property 'firstChild' of null in vue js html?

javascript - javascript中调用代码隐藏函数时如何传递参数?

javascript - 无法从组件属性设置 VueJs 组件数据值

javascript - 无法将变量输入到 Firebase 时间戳内的日期构造函数中?

vue.js - 处理 nuxt3 usefetch 上的错误

vue.js - 如何在一个类 vue.js 2 中添加 2 个条件?

javascript - 如何以 Angular 修复 404(未找到)

javascript - 日期范围过滤器不起作用

javascript - Google Charts 气泡图分类 x 和 y 轴而不是数字

javascript - Javascript 中的半边数据结构?