<style>的scope属性

本文介绍如何在Vue项目中创建并使用自定义弹框组件,包括如何解决样式覆盖问题及实现个性化样式的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这几天做一个忘记密码的功能,vue的项目,因为涉及到弹框组件,所以用vue封装了一个弹框组件,在需要的地方去引入,下面是组件的代码:

<!--
isShow type : object
key : 父组件中控制弹框显示和隐藏的属性名
value : 父组件中控制弹框显示和隐藏的属性值
title  type : string 弹框的title
titleImgSrc type : string url 弹框顶部的图片
width type : number||string 弹框的宽度 支持百分比
closeClickModel : boolean 点击遮罩层是否关闭弹框 默认true 关闭
showCloseBtn : boolean 是否显示关闭弹框按钮,默认不显示
-->
<template>
    <div v-show="isShow.value" class="vkd-dialog-box" @click.self="clickModelClose" >
        <div class="dialog" :style="style">
            <div @click="closeDialog" class="close-btn" v-if="showCloseBtn"></div>
            <div class="dialog-img" v-show="titleImgSrc">
                <img :src="titleImgSrc" alt="title image">
            </div>
            <div class="dialog-content">
                <h4 class="dialog-title">{{title}}</h4>
                <div class="dialog-body">
                    <slot></slot>
                </div>
                <div class="dialog-footer">
                    <slot name='footer'></slot>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
  export default {
    name:'vkd-Dialog',
    props:{
      'isShow':{
        type:Object,
        default:()=>{
          return{
            key:'key',
            value:false
          }
        }
      },
      'title':{
        type:String,
        default:''
      },
      'titleImgSrc':{
        type:String
      },
      'width':{
        type:[Number,String],
        default:300
      },
      'closeClickModel':{
        type:Boolean,
        default:true
      },
      'showCloseBtn':{
        type:Boolean,
        default:false
      }
    },
    methods:{
      clickModelClose(){
        this.closeClickModel&&(this.$parent[this.isShow.key]=!this.$parent[this.isShow.key]);
      },
      closeDialog(){
        this.$parent[this.isShow.key]=!this.$parent[this.isShow.key];
      }
    },
    computed:{
      style(){
        return {
          width:/^-?\d+%$/.test(this.width)?this.width:this.width+'px'
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
    .vkd-dialog-box{
        position: absolute;
        z-index: 10000;
        width:100%;
        height:100%;
        top:0;
        left:0;
        background:rgba(0,0,0,0.5);
        .dialog{
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: #fff;
            width:300px;
            .close-btn{
                width:24px;
                height:24px;
                background: url('../assets/closeBtn.png') no-repeat;
                position: absolute;
                right:15px;
                top:15px;
                cursor: pointer;
            }
            .dialog-img{
                img{
                    width: 100%;
                }
            }
            .dialog-content{
                padding:12px 30px 30px 30px;
                text-align: left;
                .dialog-title{
                    margin:0;
                    line-height: 26px;
                    font-size: 24px;
                }
                .dialog-body{
                    padding-top:12px;
                    font-size: 14px;
                    color: #767676;
                    line-height:18px;
                }
                /*.email-icon{*/
                /*display: inline-block;*/
                /*margin-right:10px;*/
                /*width:15px;*/
                /*height:10px;*/
                /*background: url("../../assets/img/login/email.png") no-repeat 0 0;*/
                /*background-size: 100% 100%;*/
                /*}*/
                /*.email{*/
                /*color:#03a9f4*/
                /*}*/
                /*.text{*/
                /*padding-top:12px;*/
                /*line-height:18px;*/
                /*color: #767676;*/
                /*}*/
                .dialog-footer{
                    margin-top:30px;
                    overflow: hidden;
                    text-align: right;
                    button{
                        text-align: center;
                        width: 65px;
                        height: 20px;
                        border-radius: 3px;
                        background-color: #fdb62b;
                        color:#fff;
                        border: none;
                        margin-left:10px;
                        font-size:14px;
                        cursor: pointer;
                    }
                    span{
                        color: #767676;
                        font-size:14px;
                    }
                }
            }
        }
    }
</style>

  在需要的组件中引入弹框组件,然后需要修改一些个性化的样式,比如title的字体颜色,发现无论怎样增加权限都没办法覆盖子组件中的样式,后来发现了问题,因为子组件和父组件的style中都使用了scope属性,浏览器渲染后,给每个组件中的元素增加了自定义属性,如图

而浏览器渲染样式的时是这样的

 

这也是scoped的工作原理,所以在子组件中写的元素,只有子组件中的自定义属性,而父组件中加的样式,最终浏览器渲染时是找不到对应的元素的,(因为父组件中样式给出的自定义属性是不一致的),所以子组件中.dialog-title的样式没办法在父组件中修改,而.dialog-footer中的button的样式是可以修改的,因为button是写在父组件中,以slot的方式插入在子组件中的,而dialog-title是通过设置props属性传递给子组件的。明白了原因后,对子组件做出了修改

如此,弹框中的header,body,footer都可以愉快地个性化样式了,但是弹框整体的风格还是需要公共组件控制的,这也是公共组件的用处吧。

另外查了下原生的scope属性,已经不支持了,所以现在只能在这些.vue的模版文件中使用,等等

 

完了,有点啰嗦。

转载于:https://www.cnblogs.com/lijianjian/p/9482587.html

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>太阳系行星数据</title> <link href="minimal-table.css" rel="stylesheet"> </head> <body> <h1>太阳系行星数据</h1> <header> <table id="table"> <caption> 太阳系中行星的一些数据。(资料取自<a href="http://nssdc.gsfc.nasa.gov/planetary/factsheet/">NASA 行星数据 - 公制</a>,图片取自<a href="https://www.nasa.gov/multimedia/imagegallery/">NASA 照片库。</a>) </caption> <thead> <td colspan="2"></td> <th scope="col" class="name-column" >名字</th> <th scope="col">图片</th> <th scope="col">质量 (10<sup>24</sup>kg)</th> <th scope="col">直径 (km)</th> <th scope="col">密度 (kg/m<sup>3</sup>)</th> <th scope="col">重力 (m/s<sup>2</sup>)</th> <th scope="col">天长 (小时)</th> <th scope="col">平均温度 (°C)</th> <th scope="col">卫星数量</th> <th scope="col">备注</th> </thead> <tbody> <tr> <th colspan="2" rowspan="4" scope="rowgroup">类地行星</th> <th>水星</th> <td>0.330</td> <td>4879</td> <td>5427</td> <td>3.7</td> <td>4222.6</td> <td>57.9</td> <td>167</td> <td>0</td> <td>距太阳最近</td> </tr> <tr> <th>金星</th> <td>4.87</td> <td>12104</td> <td>5243</td> <td>8.9</td> <td>2802.0</td> <td>108.2</td> <td>464</td> <td>0</td> <td></td> </tr> <tr> <th>地球</th> <td>5.97</td> <td>12756</td> <td>5514</td> <td>9.8</td> <td>24.0</td> <td>149.6</td> <td>15</td> <td>1</td> <td>我们的世界</td> </tr> <tr> <th>火星</th> <td>0.642</td> <td>6792</td> <td>3933</td> <td>3.7</td> <td>24.7</td> <td>227.9</td> <td>-65</td> <td>2</td> <td>红色星球</td> </tr> <tr> <th rowspan="4" scope="rowgroup">类木行星</thead> <th rowspan="2" scope="rowgroup">气巨星</th> <th>木星</th> <td>1898</td> <td>142984</td> <td>1326</td> <td>23.1</td> <td>9.9</td> <td>778.6</td> <td>-110</td> <td>67</td> <td>太阳系最大</td> </tr> <tr> <th>土星</th> <td>568</td> <td>120536</td> <td>687</td> <td>9.1</td> <td>10.7</td> <td>1433.5</td> <td>-140</td> <td>62</td> <td></td> </tr> <tr> <th rowspan="2" scope="rowgroup">冰巨星</th> <th>天王星</th> <td>86.8</td> <td>51118</td> <td>1271</td> <td>8.7</td> <td>17.2</td> <td>2872.5</td> <td>-195</td> <td>27</td> <td></td> </tr> <tr> <th>海王星</th> <td>102</td> <td>49528</td> <td>1631</td> <td>11.0</td> <td>16.1</td> <td>4495.1</td> <td>-200</td> <td>14</td> <td></td> </tr> <tr> <th colspan="2" scope="row">矮行星</th> <th>冥王星</th> <td>0.0146</td> <td>2370</td> <td>2095</td> <td>0.7</td> <td>153.3</td> <td>5906.4</td> <td>-225</td> <td>5</td> <td>2006年降格,但<a href="http://www.usatoday.com/story/tech/2014/10/02/pluto-planet-solar-system/16578959/">尚存争议</a></td> </tr> </tbody> </table> </body> </html> 这个是全部代码。我现在要在“名字”这一列添加一个整体的黑色边框应该怎么弄
最新发布
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值