Vue穿透scoped设置组件样式

为什么用组件样式穿透

        有时候希望在组件外修改组件内的样式,由于组件在style标签使用了scoped属性,外部的样式最多影响组件的根标签,没办法对组件内部具体标签进行样式修饰,这时用样式穿透就可实现这些需求。

模板定义

自定义组件my-button

<template>
	<view class="root">
		<button class="button">按钮</button>
	</view>
</template>

测试页面index.vue

<template>
	<view class="root">
		<my-button class="my-button"></my-button>
	</view>
</template>

数据声明:

  • xxx:任意标签元素元素选择器、类选择器、id选择器等都可以)

  • control-element:xxx内部需要被控制的标签元素(用元素选择器、类选择器、id选择器等都可以)

  • 如 xxx 可以是my-button.mybutton,control-element可以是button.button

组件样式穿透方式

一、>>>

style必须用CSS,使用方式如下:

xxx >>> control-element {
    /*	样式修饰	*/
}

如:

<style lang="css">
	
	.my-button >>> .button{
		background-color: green;
	}
</style>

二、/deep/

Vue2支持,style用SCSS,使用方式如下:

/*	嵌套使用	*/
xxx {
    /deep/ control-element {
        /*	样式修饰	*/
    }
}

/*	非嵌套使用	*/
xxx /deep/ control-element {
    	/*	样式修饰	*/
}

如:

<style lang="scss">
	/*	嵌套使用	*/
	.my-button {
		/deep/ .button {
			background-color: red;
		}
	}
    /*	非嵌套使用	*/
    .my-button/deep/ .button {
        background-color: red;
	}
</style>

三、::v-deep

CSS和SCSS都支持,使用方式如下:

/*  嵌套使用    */
xxx {
    ::v-deep control-element {
        /*  样式控制    */
    }
}
​
/*  非嵌套使用   */
xxx  ::v-deep  control-element {
     /* 样式修饰    */
}  

如:

<style lang="scss">
    /*  嵌套使用    */
    .my-button{
        ::v-deep .button {
            background-color: green;
        }
    } 
    /*  非嵌套使用   */
    .my-button ::v-deep .button {
        background-color: red;
    }
</style>

四、:deep()

style必须选择SCSS

穿透任意个标签

使用方式如下:

/*  嵌套使用    */
xxx {
    :deep() {
        control-element1 {
            
        }
        control-element2 {
            
        }
        ...
    }
}
/*  非嵌套使用   */
xxx :deep(){
        control-element1 {
​
        }
        control-element2 {
​
        }
        ...
}

如:

<style lang="scss" scoped>
    /*  嵌套使用    */
    .my-button {
        :deep() {
            .button {
                background-color: red;
            }
        }
     } 
    /*  非嵌套使用   */
    .my-button :deep() {
        .button {
            background-color: red;
        }
     }
</style>

穿透一种标签

使用方式如下:

/*  嵌套使用    */
xxx{
    :deep(control-element) {
        /*  样式修饰    */
    }
}
/*  非嵌套使用   */
xxx :deep(control-element) {
        /*  样式修饰    */
}

如:

<style lang="scss" scoped>
    /*  嵌套使用    */
    .my-button{
        :deep(.button) {
            background-color: green;
        }
    }
    /*  非嵌套使用   */
    .my-button :deep(.button) {
        background-color: red;
    }
</style>

Vue中,scoped样式是一种给组件样式添加作用域的方式,让样式组件内起作用,不会影响到其他组件。然而,有时候我们希望某个组件样式作用于其子组件或者父组件,这就需要穿透scoped样式。 对于子组件,我们可以通过使用`/deep/`或者`>>>`来穿透scoped样式。例如,在父组件样式中,可以这样定义: ``` <style scoped> .parent /deep/ .child { color: red; } </style> ``` 在上面的例子中,`.parent`是父组件样式,而`.child`是子组件样式。使用`/deep/`可以让`.child`样式作用于子组件。 对于父组件,如果想要穿透scoped样式,可以通过将样式定义在全局样式中,或者使用`>>>`。例如,在子组件中,可以这样定义: ``` <template> <div class="parent"> <div class="child">子组件样式</div> </div> </template> <style scoped> .child { color: red; } .parent >>> .child { color: blue; } </style> ``` 在上面的例子中,`.child`是子组件样式,`.parent`是父组件样式。使用`>>>`可以让`.child`样式作用于父组件。 需要注意的是,`/deep/`和`>>>`只在一些支持CSS Modules的构建工具中生效,例如vue-loader。如果在其他环境中使用,可能无法生效。 总结:通过使用`/deep/`或者`>>>`,我们可以穿透Vuescoped样式,让样式作用于子组件或者父组件。但是需要注意,这种方式只在部分构建工具中生效,不同环境可能会有差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值