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;
	}
}
相关推荐
当时只道寻常9 分钟前
Vue3 集成 NProgress 进度条:从入门到精通
前端·vue.js
米丘10 分钟前
Vue 3.x 单文件组件(SFC)模板编译过程解析
前端·vue.js·编译原理
米丘16 分钟前
vue3.x 内置指令有哪些?
前端·vue.js
米丘17 分钟前
Vue 3.x 模板编译优化:静态提升、预字符串化与 Block Tree
前端·vue.js·编译原理
空中海2 小时前
第一章:Vue 基础与模板语法
前端·javascript·vue.js
鼎道开发者联盟2 小时前
鼎享会 | OpenClaw Control UI 前端架构全解析:自研 UI 对接 Server 实操指南
前端·ui·架构·openclaw·control ui
许彰午3 小时前
Spring Boot + Vue 实现 XML 动态表单:固定字段 + 自由扩展方案
xml·vue.js·spring boot
前端那点事3 小时前
Vue并发控制|几十个请求高效管控(实战方案+可运行代码)
前端·vue.js
前端那点事3 小时前
Vue大批量接口请求优化|告别卡顿、超时!前端落地实战指南
前端·vue.js
空中海4 小时前
第四章:Vue Router
前端·javascript·vue.js