样式穿透和uniapp下的uni_modules里面组件的样式

文章介绍了在使用scoped属性防止样式污染时,如何通过::v-deep或/deep/解决子组件样式覆盖的问题。在Vue3和UniApp环境下,提供了修改子组件默认样式的代码示例。

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

一、原因

1、使用 scoped 后,父组件的样式将不会渗透到子组件中

2、scoped作用:避免样式污染

二、解决方案:样式穿透(修改子组件的默认样式)

1、::v-deep (必须是双冒号)【建议使用此方案:/deep/在某些时候会报错,::v-deep更保险且编译速度更快

2、/deep/

三、写法

1、vue3

::v-deep(.el-icon){ height: inherit; }

/deep/ .el-icon{ height: inherit; }

2、uniapp

::v-deep .uni-input-input {}

uni-app中同时使用SCSSscoped样式修改uni-ui件库的样式,有以下几种方法: 方法一:使用::v-deep选择器 在有scoped样式组件中,可以使用::v-deep选择器来穿透scoped样式,以修改uni-ui组件样式。例如,修改按钮组件的文字颜色背景色: ```vue <template> <view> <uni-button>按钮</uni-button> </view> </template> <script> export default { // 组件逻辑部分 } </script> <style scoped lang="scss"> /* 使用::v-deep选择器修改按钮组件样式 */ ::v-deep .uni-button { color: red; background-color: yellow; } </style> ``` 方法二:使用全局样式文件 创建一个全局样式文件,在该文件中引入uni-ui的样式,并对组件进行样式修改。然后在需要使用uni-ui组件的页面或组件中引入全局样式文件。 1. 创建一个全局样式文件,例如`global.scss`。 ```scss // global.scss // 导入uni-ui的样式文件 @import '~@dcloudio/uni-ui/lib/theme.scss'; // 修改按钮组件样式 .uni-button { color: red; background-color: yellow; } // 其他组件样式的修改... ``` 2. 在需要使用uni-ui组件的页面或组件中引入全局样式文件。 ```vue <template> <view> <uni-button>按钮</uni-button> </view> </template> <script> export default { // 组件逻辑部分 } </script> <style lang="scss"> /* 引入全局样式文件 */ @import '@/global.scss'; /* 组件样式 */ </style> ``` 方法三:使用CSS Modules 如果你的uni-app项目使用了CSS Modules,你可以在组件中使用CSS Modules来修改uni-ui组件样式。 1. 在需要使用uni-ui组件的页面或组件中,开启CSS Modules。 ```vue <template> <view> <uni-button>按钮</uni-button> </view> </template> <script> export default { // 开启CSS Modules styleIsolation: 'scoped', } </script> <style module> /* 修改按钮组件样式 */ .uni-button { color: red; background-color: yellow; } </style> ``` 通过以上方法,你可以在uni-app中同时使用SCSSscoped样式修改uni-ui组件库的样式。选择适合你项目需求的方法,并根据需要对uni-ui组件样式进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值