Home  >  Article  >  Web Front-end  >  Related methods of class and style settings in vue

Related methods of class and style settings in vue

高洛峰
高洛峰Original
2017-02-18 14:50:061463browse

class&style style setting

class

html code

<p id="box">
    <strong>阿斯顿发</strong>
</p>

css code

.red {
    color: red;
}
.gray {
    background-color: gray;
}

js code

window.onload = function() {
    new Vue({
        el: '#box',
        data: {
            red: 'red',
            gray: 'gray'
        }
    });
}

How to write the style to take effect

  1. :class="[red, gray]" The data attribute in the vue parameter is called

    <strong :class="[red, gray]"></strong>
  2. :class="{red : true, gray: true}"

    <strong :class="{red: true, gray: true}"></strong>

    The second method can also call the data attribute of the vue parameter. Remember, everything in Vue is data

    new Vue({
        el: '#box',
        data: {
           red: 'red',
           gray: 'gray',
           a: true,
           b: false
     }   
    });
    <strong :class="{red: a, gray: b}"></strong>
  3. :class="json", a simplified version of the second method, the official version

    new Vue({
        el: '#box',
        data: {
            red: 'red',
            gray: 'gray',
            json: {
                a: true,
                b: false
            }
        }
    });
<strong :class="json"></strong>

style

is basically the same as class

  1. :style="{}"

<strong :style="{color: &#39;red&#39;, backgroundColor: &#39;gray&#39;}"></strong>
  1. :style="a"

    new Vue({
        el: '#box',
        data: {
            red: 'red',
            gray: 'gray',
            a: {
                color: 'red',
                backgroundColor: 'gray' //注意复合样式的书写规范
            }
        }
    });
    <strong :style="a">阿斯顿发</strong>
  2. :style="[a, b]", a, b correspond to the two json data in data

More class and style in vue For relevant setting methods and related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:CSS3 text wrappingNext article:CSS3 text wrapping