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;
	}
}
相关推荐
正在学习前端的---小方同学14 分钟前
vue-easy-tree树状结构
前端·javascript·vue.js
毕业设计制作和分享1 小时前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis
前端开发爱好者8 小时前
尤雨溪官宣:"新玩具" 比 Prettier 快 45 倍!
前端·javascript·vue.js
欧阳呀8 小时前
Vue+element ui导入组件封装——超级优雅版
前端·javascript·vue.js·elementui
Element_南笙8 小时前
吴恩达新课程:Agentic AI(笔记2)
数据库·人工智能·笔记·python·深度学习·ui·自然语言处理
SmartSoftHelp开发辅助优化9 小时前
C# WinForm 编程高手:程序,进程,线程。程序,窗体,UI,后台。是如何协调工作的?深度解析>SmartSoftHelp魔法精灵工作室
microsoft·ui·c#
华仔啊11 小时前
用 Vue3 + Canvas 做了个超实用的水印工具,同事都在抢着用
前端·vue.js·canvas
Q_Q51100828511 小时前
python+uniapp基于微信小程序团购系统
spring boot·python·微信小程序·django·uni-app·node.js·php
炒毛豆11 小时前
uniapp微信小程序+vue3基础内容介绍~(含标签、组件生命周期、页面生命周期、条件编译(一码多用)、分包))
vue.js·微信小程序·uni-app
岁月宁静12 小时前
在 Vue 3.5 中优雅地集成 wangEditor,并定制“AI 工具”下拉菜单(总结/润色/翻译)
前端·javascript·vue.js