vue与style绑定

来源:互联网 发布:全国信息技术水平java 编辑:程序博客网 时间:2024/06/14 06:56

在vue中,我们通常使用动态值来设置style,比如:动态设置background-image.我们需要修改url的值,我试过不少办法

    <p style="background:{{color}}"></p>    <p style="background"></p>    computed:{        background () {            return "background:" + this.color        }    }

这些写法都不对,或者得不到想要的结果
后来通过学习,我们知道应该使用事件绑定的方式来动态设置style

    <p v-bind:style="{background:color}"></p>    <p v-bind:style="{background:${color}}"></p>

当前我都是使用最简单的方式写动态的style:

    <p :style="{backgroundImage:`url(${url}) no-repeat top center`}"></p>

一个冒号:就算是代表了v-bind方式了。

原创粉丝点击