【发布时间】:2020-12-17 07:00:09
【问题描述】:
我正在创建一个 Vue 组件,它根据给定的图像名称或键(由 API 提供)显示来自我的节点模块的 SVG 图像。
如果我像~cryptocurrency-icons/svg/color/eur.svg一样直接放置源图像,则资源加载。
但如果我尝试使用 props 或使用 :src="imageSource" 分配它的安装方法来计算它,它不会加载。
我是 Vue 新手,所以我不知道为什么会这样?我应该使用下载到公共目录而不是 NPM 包中的图像吗?
<template lang="html">
<img src="~cryptocurrency-icons/svg/color/eur.svg" alt="icon" />
</template>
<script lang="js">
export default {
name: 'crypto-icon',
props: ['currency'],
mounted() {
this.imageSource = `~cryptocurrency-icons/svg/color/${this.currency}.svg`
},
data() {
return {
imageSource: ""
}
},
methods: {
getImageResource(){
return `~cryptocurrency-icons/svg/color/${this.currency}.svg`
}
},
computed: {
}
}
</script>
<style lang="scss" scoped>
.crypto-icon {}
</style>
【问题讨论】:
-
试试
this.imageSource =require(~cryptocurrency-icons/svg/color/${this.currency}.svg)
标签: node.js vue.js vuejs2 node-modules vue-loader