uniapp【uni-ui】【vue3】样式覆盖方式记录

vue3中推荐使用:deep进行样式覆盖与穿透

javascript 复制代码
<template>
	<uni-easyinput v-model="value" placeholder="请输入内容"></uni-easyinput>
</template>

<script lang="ts" setup>
	import { ref } from 'vue'
	const value = ref('')
</script>

<style lang="scss" scoped>
	:deep(.uni-easyinput__content-input){
		border: 1px solid red;
	}
</style>

对于/deep/ 是vue2中的样式穿透方案,现已弃用(于vue3中将不会生效)

对于::v-deep是vue3早期的样式穿透方案,现已弃用(但是会生效只是警告而已)

对于uni-ui的样式覆盖注意

直接将类名放置在标签上,将导致样式覆盖无法生效。如:

javascript 复制代码
<template>
	<uni-easyinput class="uni-input-outer" v-model="value" placeholder="请输入特殊样式框内容"></uni-easyinput>
</template>

<script lang="ts" setup>
	import { ref } from 'vue'
	const value = ref('')
</script>

<style lang="scss" scoped>
.uni-input-outer {
	:deep(.uni-easyinput__content-input){
		border: 1px solid red;
	}
}
</style>

我们通常希望给标签加上类名用于样式隔绝,专门覆盖某一个输入框的样式。但直接在uni-ui上的标签上加样式时,会出现覆盖样式不生效。此时的解决方案是在外层加个view标签包裹即可(注: 在组件中覆盖样式也不生效,需要把样式写在page页面的style里面或全局样式里面。尝试用全局方式或创建样式文件并使用@import ''引入的方式管理这些覆盖样式)。

示例如:

index.vue

javascript 复制代码
<template>
	<view class="uni-input-outer">
		<uni-easyinput v-model="value" placeholder="请输入特殊样式框内容"></uni-easyinput>
	</view>
</template>
<script lang="ts" setup>
	import { ref } from 'vue'
	const value = ref('')
</script>
<style lang="scss" scoped>
.uni-input-outer {
	:deep(.uni-easyinput__content-input){
		border: 1px solid red;
	}
}
</style>

亦或者

index.vue

javascript 复制代码
<template>
	<view class="uni-input-outer">
		<uni-easyinput v-model="value" placeholder="请输入特殊样式框内容"></uni-easyinput>
	</view>
</template>
<script lang="ts" setup>
	import { ref } from 'vue'
	const value = ref('')
</script>
<style lang="scss" scoped>
	@import './index.scss';
</style>

index.scss

javascript 复制代码
.uni-input-outer {
	:deep(.uni-easyinput__content-input){
		border: 1px solid red;
	}
}
相关推荐
AC赳赳老秦2 分钟前
DeepSeek 辅助科研项目申报:可行性报告与经费预算框架的智能化撰写指南
数据库·人工智能·科技·mongodb·ui·rabbitmq·deepseek
毎天要喝八杯水1 小时前
搭建vue前端后端环境
前端·javascript·vue.js
Dontla3 小时前
Axure RP(Rapid Prototyper)原型图设计工具介绍
ui·axure·photoshop
晚霞的不甘4 小时前
Flutter for OpenHarmony从基础到专业:深度解析新版番茄钟的倒计时优化
android·flutter·ui·正则表达式·前端框架·鸿蒙
东东5164 小时前
果园预售系统的设计与实现spingboot+vue
前端·javascript·vue.js·spring boot·个人开发
怪兽毕设4 小时前
基于SpringBoot的选课调查系统
java·vue.js·spring boot·后端·node.js·选课调查系统
Amumu121384 小时前
Vue Router(一)
前端·javascript·vue.js
郑州光合科技余经理5 小时前
可独立部署的Java同城O2O系统架构:技术落地
java·开发语言·前端·后端·小程序·系统架构·uni-app
雪芽蓝域zzs5 小时前
uniapp 取消滚动条
uni-app
切糕师学AI5 小时前
VSCode 下如何检查 Vue 项目中未使用的依赖?
vue.js·vscode