【Vue3】2-9 : class样式与style样式的三种形态

 本书目录:点击进入

一、标签样式( class 和 style)

二、实战

>  代码

>  效果


一、标签样式( classstyle

        在将 v-bind 用于 classstyle 时,字符串拼接麻烦且易错,Vue.js 做了专门的增强。表达式结果的类型除了字符串之外,还可以是对象或数组

1.1、写法

  • v-bind:class  简写 :class

  • v-bind:style  简写 :style

let vm = Vue.createApp({
    data() {
        return {
            myClass1: 'box box2',
            myClass2: ['box', 'box2'], //推荐
            myClass3: { box: true, box2: true },//推荐
            myStyle1: 'background: red; color: white;',
            myStyle2: ['background: red', 'color: white'],//推荐
            myStyle3: { background: 'red', color: 'white' },//推荐
        }
    },
}).mount("#app")

二、实战

示例:2秒后

  • 改变 aaaaa  变 aaaaa

  • 改变 bbbbb  变 bbbbb

>  代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box1{ background: red;}
    .box2{ color: white;}
  </style>
  <script src="../vue.global.js"></script>
</head>
<body>
  <div id="app">
    <div :class="myClass">aaaaa</div>
    <div :style="myStyle">bbbbb</div>
  </div>
  <script>

    let vm = Vue.createApp({
      data(){
        return {
          //myClass: 'box1 box2'
          //myClass: ['box1', 'box2'],
          myClass: { box1: true, box2: true },
          //myStyle: 'background: blue; color: yellow',
          //myStyle: ['background: blue', 'color: yellow'],
          myStyle: { background: 'blue', color: 'yellow' },
        }
      }
    }).mount('#app');

    setTimeout(()=>{
      
      //vm.myClass.pop();
      vm.myClass.box2 = false;

      //vm.myStyle.push('width: 300px');
      vm.myStyle.background = 'pink';

    }, 2000)

  </script>
</body>
</html>
>  效果

相关推荐

  1. vue 动态添加style样式

    2024-01-13 01:04:02       43 阅读
  2. VUE3Uniapp Class变量和内联样式

    2024-01-13 01:04:02       9 阅读
  3. Vue 绑定class样式

    2024-01-13 01:04:02       15 阅读
  4. vue样式

    2024-01-13 01:04:02       6 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-13 01:04:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-13 01:04:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-13 01:04:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-13 01:04:02       18 阅读

热门阅读

  1. 【web安全】弱口令,以及不同领域的弱口令爆破

    2024-01-13 01:04:02       41 阅读
  2. autox.js嘎嘎牛p的悬浮窗模板

    2024-01-13 01:04:02       39 阅读
  3. 4 微信小程序

    2024-01-13 01:04:02       33 阅读
  4. 深入理解区间合并:让数字之间的故事更加有序

    2024-01-13 01:04:02       34 阅读
  5. linux系统nginx工具的一些应用

    2024-01-13 01:04:02       34 阅读
  6. C# 快速模指数运算 快速求余运算

    2024-01-13 01:04:02       34 阅读
  7. Linux中关于文件权限详解

    2024-01-13 01:04:02       37 阅读
  8. checkpoint存的是参数还是模型?

    2024-01-13 01:04:02       37 阅读
  9. 面试 React 框架八股文十问十答第六期

    2024-01-13 01:04:02       31 阅读