当前位置: 首页 > news >正文

Vue3学习笔记(9.1)

Vue.js style(内联样式)

我们可以在v-bind:style直接设置样式,可以简写:style

<!--* @Author: RealRoad1083425287@qq.com* @Date: 2023-04-02 19:41:53* @LastEditors: Mei* @LastEditTime: 2023-04-03 15:41:44* @FilePath: \vscode\Vue3_listen.html* @Description: * * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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><script src="vue_doc/vue.global3.js"></script><style>.static{width: 100px;height:100px;background:purple}.active{/* width:100px;height:100px; */background:blue;}.text_danger{background: red;}</style></head>
<body><div id="app"><div :style="{color:activeColor,fontSize:fontSize+'px'}" >基尼太美</div></div><!-- <p id="info"></p> --><script>const app={data(){return{// counter:1// kilometers:0,// meters:0isActive:true,hasError:true,error:null,activeClass:'active',errorClass:'text_danger',activeColor:'red',fontSize:30}}// computed:{//     classObject(){//         return{//             active:this.isActive && !this.error,//             'text-danger':this.error&&this.error.type==='fatal'//         }//     }// }}Vue.createApp(app).mount('#app')// vm.$watch('kilometers',function(a,b){//     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'// })</script>
</body>
</html>

 也可以直接绑定到一个样式对象

<!--* @Author: RealRoad1083425287@qq.com* @Date: 2023-04-02 19:41:53* @LastEditors: Mei* @LastEditTime: 2023-04-03 15:45:53* @FilePath: \vscode\Vue3_listen.html* @Description: * * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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><script src="vue_doc/vue.global3.js"></script><style>.static{width: 100px;height:100px;background:purple}.active{/* width:100px;height:100px; */background:blue;}.text_danger{background: red;}</style></head>
<body><div id="app"><div :style="styleObject" >基尼太美</div></div><!-- <p id="info"></p> --><script>const app={data(){return{// counter:1// kilometers:0,// meters:0// isActive:true,// hasError:true,// error:null,activeClass:'active',errorClass:'text_danger',styleObject:{color:'purple',fontSize:"30px",}}}// computed:{//     classObject(){//         return{//             active:this.isActive && !this.error,//             'text-danger':this.error&&this.error.type==='fatal'//         }//     }// }}Vue.createApp(app).mount('#app')// vm.$watch('kilometers',function(a,b){//     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'// })</script>
</body>
</html>

 v-bind:style可以使用数组将多个样式对象应用到一个元素上:
 

<!--* @Author: RealRoad1083425287@qq.com* @Date: 2023-04-02 19:41:53* @LastEditors: Mei* @LastEditTime: 2023-04-03 15:49:52* @FilePath: \vscode\Vue3_listen.html* @Description: * * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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><script src="vue_doc/vue.global3.js"></script><style>.static{width: 100px;height:100px;background:purple}.active{/* width:100px;height:100px; */background:blue;}.text_danger{background: red;}</style></head>
<body><div id="app"><div :style="[styleObject,secondStyle]" >基尼太美</div></div><!-- <p id="info"></p> --><script>const app={data(){return{// counter:1// kilometers:0,// meters:0// isActive:true,// hasError:true,// error:null,activeClass:'active',errorClass:'text_danger',styleObject:{color:'purple',fontSize:"30px",},secondStyle:{'font-weight':'bold'}}}// computed:{//     classObject(){//         return{//             active:this.isActive && !this.error,//             'text-danger':this.error&&this.error.type==='fatal'//         }//     }// }}Vue.createApp(app).mount('#app')// vm.$watch('kilometers',function(a,b){//     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'// })</script>
</body>
</html>

这样,基尼太美就加粗了。

 注意:当v-bind:style使用需要特定前缀的CSS属性时,如transform,Vue.js会自动侦测并添加相应的前缀。

多重值

可以为style绑定中的property提供一个包含多个值的数组,常用于提供多个带前缀的值,例如:

<div :style="{display:['-webkit-box','-ms-flexbox','flex']}"></div>

这样写只会渲染数组中最后一个被浏览器支持的值。如果浏览器支持不带浏览器前缀的flexbox,那么就只会渲染display:flex。

组件上使用class属性

当我们在带有单个根元素的自定义组件上使用class属性,这些class将被添加到该元素中。此元素上的现有class将不会被覆盖。

<!--* @Author: RealRoad1083425287@qq.com* @Date: 2023-04-02 19:41:53* @LastEditors: Mei* @LastEditTime: 2023-04-03 16:04:54* @FilePath: \vscode\Vue3_listen.html* @Description: * * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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><script src="vue_doc/vue.global3.js"></script><style>.static{width: 100px;height:100px;background:purple}.active{/* width:100px;height:100px; */background:blue;}.text_danger{background: red;}</style></head>
<body><div id="app"><!-- <div :style="[styleObject,secondStyle]" >基尼太美</div> --><mez class="classC classD"></mez></div><!-- <p id="info"></p> --><script>const app=Vue.createApp({})app.component('mez',{template:'<h1 class="classA classB">Let is lie</h1>'})app.mount('#app')// vm.$watch('kilometers',function(a,b){//     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'// })</script>
</body>
</html>

 对于带数据绑定class也同样适用:

<my-component :class="{active:isActive}"></my-component>

当isActive为TRUE时,HTML将被渲染成为:

<p class="active">Hi</p>

如果你的组件有多个根元素,你需要定义哪部分将接收这个类。可以使用$attrs组件属性执行此操作:

<!--* @Author: RealRoad1083425287@qq.com* @Date: 2023-04-02 19:41:53* @LastEditors: Mei* @LastEditTime: 2023-04-04 08:54:29* @FilePath: \vscode\Vue3_listen.html* @Description: * * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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><script src="vue_doc/vue.global3.js"></script><style>.static{width: 100px;height:100px;background:purple}.active{/* width:100px;height:100px; */background:blue;}.text_danger{background: red;}.classA{color:coral;font-size: 30px;}.classB{color:rgb(255, 80, 235);font-size: 30px;}</style></head>
<body><div id="app"><!-- <div :style="[styleObject,secondStyle]" >基尼太美</div> --><mez class="classA"></mez></div><!-- <p id="info"></p> --><script>const app=Vue.createApp({})app.component('mez',{template:`<h1 :class="$attrs.class">Let is lie</h1><span>这是一个子组件</span>`})app.mount('#app')// vm.$watch('kilometers',function(a,b){//     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'// })</script>
</body>
</html>

 

http://www.lryc.cn/news/45494.html

相关文章:

  • MinIO 环境变量泄漏漏洞(CVE-2023-28432)
  • 数组转字符串、字符串转数组的方法
  • local fault和remote fault
  • 二叉树搜索树 AVL树
  • nginx配置代理多个前端资源
  • SuperMap iServer下载安装,启用服务,以及发布服务
  • vxe-table简单使用 vue vxe-table vue整合vxe-table vue2 vxe-table 简单使用
  • Vue项目的打包上线步骤
  • 都2023了,学习自动化测试还有必要么?会不会浪费我时间
  • 银行数字化转型导师坚鹏:如何有效推进银行数字化转型工作
  • 【MySQL高级篇】第09章_性能分析工具的使用
  • 关于xhtml和html的区别
  • 原生JavaScript比较两个日期大小,项目中通用
  • 【JAVA真的没出路了吗?】
  • PCB模块化设计11——VGA高速PCB布局布线设计规范
  • 【Python】【进阶篇】五、Python爬虫的抓取网页
  • docker安装MongoBD(超详细)
  • 6轴陀螺仪姿态解算
  • 提升集群吞吐量与稳定性的秘诀: Dubbo 自适应负载均衡与限流策略实现解析
  • 大数据分析工具Power BI(十七):制作过程分析和原因分析图表
  • 公司“007”式工作的卷王测试员,被辞退了…
  • C++ Primer第五版_第七章习题答案(1~10)
  • 2023年全国最新保安员精选真题及答案42
  • 通过 DVT 和 dbt 测试监控Airbyte数据管道
  • BootStrap4:组件
  • 菜鸟也能在10分钟内开发出3D数字化城市,这份干货教程请收好!
  • 【区块链技术开发】十个比较流行的以太坊智能合约开发框架
  • Linux三剑客之grep命令详解
  • 【Python】【进阶篇】二、Python爬虫的User-Agent用户代理
  • ORBSLAM3 --- 双目惯导执行ORBSLAM3(一):Stereo_intertail_euroc.cc文件解析