vue3.x class和style绑定

 

注:实例环境 vue cli构建的项目

app.vue

<template>
  <Example></Example>
</template>

<script>
import Example from './components/Example'

export default {
  name: 'App',
  components: {
    Example
  }
}
</script>

<style>

</style>

Example.vue

<template>
    <div>
       <div :class="className" :style="styleName">
           <div>
               <button @click="changeClass('class1')">灰色</button>
               <button @click="changeClass('class2')">黑色</button>
               <button @click="changeClass('class3')">红色</button>
               &nbsp;&nbsp;<span>|</span>&nbsp;&nbsp;
               <button @click="changeStyle('left')">居左</button>
               <button @click="changeStyle('center')">居中</button>
               <button @click="changeStyle('right')">居右</button>
           </div>
           <p>
               class 绑定
           </p>

       </div>
    </div>
</template>

<script>
    export default {
        name: "Example",
        data:function () {
            return {
                className: 'class1',
                styleName: 'left'
            }
        },
        methods:{
            changeClass:function (className) {
                this.className = className;
            },
            changeStyle:function (styleName) {
                this.styleName ={
                    textAlign: styleName
                };
            }
        }
    }
</script>

<style scoped>
.class1{
    background-color: #ccc;
    color:#000;
}
.class2{
    background-color: #000;
    color:#fff;
}
.class3{
    background-color: red;
    color: yellow;
}
</style>

刷新浏览器

vue3.x class和style绑定

上一篇:ASP.NET相关技术整理


下一篇:第九节 winserver2008 r2 溢出提权