javascript - 将图像名称传递给vue组件时找不到图像
问题描述
我正在尝试动态创建输入组件,因为我需要为每个输入提供一个特定的图标。图标设置为输入作为背景图像,当我像这样直接在css中使用它时它可以工作background-image: url(../../assets/icons/email.svg);
<template>
<section class="input-box">
<input class="input-field" type="text" :style="imgIcon">
<label>{{title}}</label>
</section>
</template>
<script>
export default {
props: ["title", "bgImg"],
computed: {
imgIcon() {
return 'background-image: url(./assets/icons/' + this.bgImg + ')';
}
}
}
</script>
当我将名称作为字符串传递时
<custome-input title="password" bgImg="'p_key.svg'"></custome-input>
错误已解决,但没有任何显示!
究竟有什么问题?
解决方案
如果您想使用模板中的图像,您需要使用@
并且url
应该是一个字符串。
export default {
props: ["title", "bgImg"],
computed: {
imgIcon() {
return `background-image: url('@/assets/icons/${this.bgImg}')`;
}
}
}
推荐阅读
- python - SyntaxError: Name 's' is used before global declaration in Python/Flask 代码
- javascript - 如何在功能成功反应原生后立即启动新功能
- openstreetmap - OpenStreetMap API - 查找和可视化砾石路
- ms-access - Microsoft Access 通过组合框筛选查询
- c# - System.Net.Mail.SmtpException:发送邮件失败--->无法将数据写入传输连接:现有连接被强行关闭
- javascript - React Router 改变了 URL 但不重新渲染组件
- typescript - 如何使用 react native 和 typescript 设置状态?
- asp.net-web-api - 尝试写入文件时访问被拒绝
- nmap - NMap --script memcached-info 不适用于 UDP
- azure - Management.Azure.com REST API 调用与 Odata $expand 和 $filter | 正文结果未过滤