javascript - Vue.js 无法从方法中添加样式
问题描述
我想从方法中将一些样式添加到 html 元素中:
<div class="add-profile-img" v-bind:style="getBackgroundImg()">
方法是:
getBackgroundImg: function() {
return {
width: 180px;
height: 180px;
background-color: 'yellow';
background-image:url(this.BASE_URL +'/uploads/noimg.gif');
}
},
但是,我得到
Syntax Error: Identifier directly after number (79:13)
77 | getBackgroundImg: function() {
78 | return {
> 79 | width: 180px;
| ^
我该如何解决?
解决方案
以像素为单位的维度需要采用字符串格式,以便函数返回有效的 javascript 对象:
return {
width: '180px',
height: '180px',
'background-color': 'yellow',
'background-image': `url(${this.BASE_URL}/uploads/noimg.gif)`
}
推荐阅读
- python - AWS IoT shadowGet Timeout
- reactjs - “错误:不是窗口”通过 Instagram 应用浏览器
- git - Bitbucket 存储库分支克隆问题
- vue.js - Vuex 状态随突变而变化 - apollo graphql 查询
- html - 选项标签内的隐藏值
- python - 使用 PySpark mapPartitions 进行异步函数调用
- mime-types - CodeIgniter 4 无法设置内容类型
- android - 通过 Android WebAuthn 禁用漫游身份验证器
- javascript - 在 Javascript 中对 3 步密码进行解密会导致字母表中的 26 个字符中只有一半被显示,而一些字符最终会显示为符号
- flutter - 无法连接到 Flutter 和 Android Studio 中的 Gradle 守护进程