VUE开发中/deep/的使用 其他写法v-deep:: vue3中写法 :deep(.img)

本文介绍Vue中样式穿透的两种方式:/deep/与>>>的选择器使用方法及其原理,并附带CSS优先级提升技巧(!important)。

实际开发中经常会遇到一种情况,一个功能需求例如表格,可以直接使用element等组件库的表格组件,省时省力。
在这里插入图片描述
但实际需求总不会是和组件完全一样的,UI会有各种天马行空的样式给你,例如这种表格样式在这里插入图片描述
这时候有两条路可选

  • 自己用原生手写一个,但对于初级菜鸟来讲极不友好,会浪费大量时间走弯路
  • 对组件原生样式进行改写

而改写样式就需要用到/deep/样式穿透了

/deep/

VUE官方

  • 为了保证每个组件的css唯一性,避免污染全局或者被全局污染,vue提供了scoped作用域。
  • 这样在打包的时候会生成一个独一无二hash值,这样父组件的样式就不会影响到子组件了。
  • 只修改这个地方的样式,在其他页面用到本组件的时候不变,就可以用/deep/了
  • 把 /deep/ 换成 >>>,也可以达到同样的效果。
<template>
    <el-table  
    	...
        class="table" 
    ></el-table>
</template>

<style scoped>
.table /deep/ th{
    text-align: center;
    padding: 4px;
}
.table /deep/ td{
    border: none;
    padding: 0;
}
.table /deep/ .cell{
    padding: 0;
    margin:0px 6px;
}
</style>

但注意,/deep/选择器这个是chrome浏览器自己的标准,其他浏览器并没有实现这个标准,所以其他浏览器是失效的。

补充: !import

CSS写在不同的地方有不同的优先级,加入了!importcss样式优先级最高

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>测试Css中的!Important区别</title> 
</head> 
<style type="text/css">
.testClass{ 
color:blue !important;
}
</style>
<body>
    <div class="testClass" style="color:red;">
        测试Css中的Important
    </div>
</body>
</html>
PS C:\Users\30424\Desktop\王凯毕设项目\newvue\vue> npm run serve > vue@0.1.0 serve > vue-cli-service serve INFO Starting development server... [51%] building (242/377 modules) [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead of ::v-deep <inner-selector>. [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead of ::v-deep <inner-selector>. [51%] building (300/389 modules) [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead of ::v-deep <inner-selector>. [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead of ::v-deep <inner-selector>. [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead of ::v-deep <inner-selector>. [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead of ::v-deep <inner-selector>. [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead of ::v-deep <inner-selector>. ERROR Failed to compile with 1 error 23:11:11 error in ./src/views/Reports.vue?vue&type=template&id=ff4e9e16&scoped=true Module not found: Error: Can&#39;t resolve &#39;@/assets/images/knowledge_graph.png&#39; in &#39;C:\Users\30424\Desktop\王凯毕设项目\newvue\vue\src\views&#39; ERROR in ./src/views/Reports.vue?vue&type=template&id=ff4e9e16&scoped=true (./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/templateLoader.js??ruleSet[1].rules[3]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/Reports.vue?vue&type=template&id=ff4e9e16&scoped=true) 2:0-61 Module not found: Error: Can&#39;t resolve &#39;@/assets/images/knowledge_graph.png&#39; in &#39;C:\Users\30424\Desktop\王凯毕设项目\newvue\vue\src\views&#39; @ ./src/views/Reports.vue?vue&type=template&id=ff4e9e16&scoped=true 1:0-280 1:0-280 @ ./src/views/Reports.vue 1:0-80 8:68-74 18:79-20:3 19:29-35 18:2-20:4 @ ./src/router/index.js 9:0-43 41:13-20 @ ./src/main.js 5:0-30 10:2-8 webpack compiled with 1 error
05-20
[HBuilder] 16:00:52.891 Vue2 scss 预编译器默认已由 node-sass 更换为 dart-sass,如果您的代码使用了 dart-sass 不支持的旧语法,可能存在部分不兼容的问题。 [HBuilder] 16:00:52.901 解决方案: [HBuilder] 16:00:52.919 方案1:调整为 dart-sass 支持的语法,详情:https://uniapp.dcloud.net.cn/tutorial/syntax-css.html#css-preprocessor [HBuilder] 16:00:52.932 方案2:如果您希望继续使用node-sass,您可以在 manifest.json 中配置 "sassImplementationName": "node-sass",详情:https://uniapp.dcloud.net.cn/tutorial/syntax-css.html#css-preprocessor [HBuilder] 16:00:52.945 Module build failed (from ./node_modules/@dcloudio/vue-cli-plugin-uni/packages/sass-loader/dist/cjs.js): [HBuilder] 16:00:52.958 SassError: expected "+", "-", "*", "/", or ")". [HBuilder] 16:00:52.971 ╷ [HBuilder] 16:00:52.981 432 │ margin-top: calc(var(--status-bar-height); [HBuilder] 16:00:52.991 │ ^ [HBuilder] 16:00:53.000 ╵ [HBuilder] 16:00:53.010 F:\运维服务中心\闽电通\apk\mdt-app\pages\recruitment\candidate\findJob.vue 432:45 root stylesheet [HBuilder] 16:00:53.020 Vue2 scss 预编译器默认已由 node-sass 更换为 dart-sass,如果您的代码使用了 dart-sass 不支持的旧语法,可能存在部分不兼容的问题。 [HBuilder] 16:00:53.028 解决方案: [HBuilder] 16:00:53.038 方案1:调整为 dart-sass 支持的语法,将深度选择器 /deep/ 调整为 ::v-deep,详情:https://uniapp.dcloud.net.cn/tutorial/syntax-css.html#css-preprocessor [HBuilder] 16:00:53.048 方案2:如果您希望继续使用node-sass,您可以在 manifest.json 中配置 "sassImplementationName": "node-sass",详情:https://uniapp.dcloud.net.cn/tutorial/syntax-css.html#css-preprocessor [HBuilder] 16:00:53.059 Module build failed (from ./node_modules/@dcloudio/vue-cli-plugin-uni/packages/sass-loader/dist/cjs.js): [HBuilder] 16:00:53.069 SassError: expected selector. [HBuilder] 16:00:53.079 ╷ [HBuilder] 16:00:53.089 254 │ /deep/.rich-text img{ [HBuilder] 16:00:53.098 │ ^ [HBuilder] 16:00:53.107 ╵ [HBuilder] 16:00:53.117 F:\运维服务中心\闽电通\apk\mdt-app\pages\electricitySales\newsDetail.vue 254:1 root stylesheet [HBuilder] 16:00:53.126 Vue2 scss 预编译器默认已由 node-sass 更换为 dart-sass,如果您的代码使用了 dart-sass 不支持的旧语法,可能存在部分不兼容的问题。 [HB
04-01
<template> <div> <van-nav-bar title="商城主页" left-text="返回" left-arrow right-text="用户登录" /> <!-- 2.轮播图功能 --> <van-swipe class="my-swipe" :autoplay="3000" lazy-render> <van-swipe-item v-for="(item,index) in swiperList" :key="index"> <img :src="item.carouselUrl" /> </van-swipe-item> </van-swipe> <!-- 商品列表功能 --> <!-- column-num="5"设置一排显示多少个 --> <van-grid :column-num=5 > <van-grid-item v-for="(item,index) in categoryList" :key="index" > <img :src="item.imgUrl" alt=""> <span>{{item.name}}</span> </van-grid-item> </van-grid> </div> </template> <script> import Vant from &#39;vant&#39;; import axios from &#39;axios&#39; export default { // 初始化数据 data(){ return{ swiperList:[], // 商品列表的数据 categoryList: [ { name: &#39; 超市&#39;, imgUrl: &#39;//s.weituibao.com/1583585285461/cs.png&#39;, categoryId: 100001 }, { name: &#39; 服饰&#39;, imgUrl: &#39;//s.weituibao.com/1583585285468/fs.png&#39;, categoryId: 100003 }, { name: &#39;全球购&#39;, imgUrl: &#39;//s.weituibao.com/1583585285470/qq.png&#39;, categoryId: 100002 }, { name: &#39; 生鲜&#39;, imgUrl: &#39;//s.weituibao.com/1583585285472/sx.png&#39;, categoryId: 100004 }, { name: &#39; 到家&#39;, imgUrl: &#39;//s.weituibao.com/1583585285467/dj.png&#39;, categoryId: 100005 }, { name: &#39;充值缴费&#39;, imgUrl: &#39;//s.weituibao.com/1583585285465/cz.png&#39;, categoryId: 100006 }, { name: &#39;9.9元拼&#39;, imgUrl: &#39;//s.weituibao.com/1583585285469/pt.png&#39;, categoryId: 100007 }, { name: &#39;领劵&#39;, imgUrl: &#39;//s.weituibao.com/1583585285468/juan.png&#39;, categoryId: 100008 }, { name: &#39;省钱&#39;, imgUrl: &#39;//s.weituibao.com/1583585285471/sq.png&#39;, categoryId: 100009 }, { name: &#39;全部&#39;, imgUrl: &#39;//s.weituibao.com/1583585285470/qb.png&#39;, categoryId: 100010 } ] } }, async created(){ const{data}= await axios.get(&#39;/index-infos&#39;) this.swiperList=data.carousels; console.log(this.swiperList) }, }; </script> <style lang="less" scoped> // 文字和金额样式----------------- .good-desc { text-align: center; font-size: 14px; padding: 10px 0; } .title { color: #222333; } .price { color: #1baeae; } // 轮播图样式------------------------ .my-swipe .van-swipe-item { img { width: 100%; height: 100%; } } .category-grid { // 确保网格项正确排列 ::v-deep .van-grid-item__content { padding: 10px 5px; background-color: transparent; } } </style> 这个里的column-num没有其效果怎么回事
10-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

跳动的世界线

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值