为了账号安全,请及时绑定邮箱和手机立即绑定

'-' 在 v-bind:style / Vue.js 中

'-' 在 v-bind:style / Vue.js 中

哔哔one 2022-01-07 14:07:57
我无法理解如何将 CSS 代码制作成带有符号“-”的 v-bind:style。如果我尝试做这样的事情:<DIV style="width:100px;height: 100px;background-color: red;cursor: pointer;" v-bind:style="{ margin-left: margin + 'px'}"></DIV>我得到:invalid expression: Unexpected token '-' in
查看完整描述

1 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

正如Vue文档中所解释的:“您可以使用 camelCase 或 kebab-case(在 kebab-case 中使用引号)作为 CSS 属性名称”


因此,您需要将其更改margin-left为marginLeftOR'margin-left'以使其按预期工作。


您的代码将变为:


<div style="width:100px;height: 100px;background-color: red;cursor: pointer;" 

     v-bind:style="{ 'margin-left': margin + 'px'}">

     ...

</div>

或者


<div style="width:100px;height: 100px;background-color: red;cursor: pointer;" 

     v-bind:style="{ marginLeft: margin + 'px'}">

    ...

</div>


希望这可以帮助!


查看完整回答
反对 回复 2022-01-07
  • 1 回答
  • 0 关注
  • 272 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号