vue3使用深度選擇器修改樣式問題
vue3使用深度選擇器修改樣式
解決警告:
[@vue/compiler-sfc] the >>> and /deep/ combinators have been deprecated. Use :deep() instead.
vue3已棄用以往的深度選擇器
- 警告: the >>> and /deep/ combinators have been deprecated. Use :deep() instead. 翻譯過來就是 “>>>和/deep/已經(jīng)被棄用。使用方法:deep()。”
- 雖然以往的>>>和/deep/依然可以使用,但是會出現(xiàn)警告語句提示
- 要是不想看到警告的話可以使用 “:deep()” 即可
例子:
/deep/ .el-button.is-round { ? ? padding: 4px 20px; ? ? background-color: #109cf1; } /*改為如下書寫方式即可 */ :deep(.el-button.is-round) { ? ? padding: 4px 20px; ? ? background-color: #109cf1; }
當(dāng)有并集選擇器,直接把類名放入 :deep() 括號中是不太對的:
/*如下修改只能改到第一個類名,修改不到第二個類名 */ :deep(.el-input__inner, .el-button.is-round) { ? ? width: 440px; ? ? height: 60px; ? ? line-height: 20px; ? ? border-radius: 30px; ? ? border: 1px solid rgba(211, 211, 211, 100); ? ? font-size: 16px; } /*應(yīng)改為 */ :deep(.el-input__inner), :deep(.el-button.is-round) { ? ? width: 440px; ? ? height: 60px; ? ? line-height: 20px; ? ? border-radius: 30px; ? ? border: 1px solid rgba(211, 211, 211, 100); ? ? font-size: 16px; }
vue如何使用深度選擇器?
在作用域 css 中,如果您需要使用深度選擇器(即來自父作用域 css)修改子組件的樣式,那么您需要使用>>>組合器。
例如,父作用域 css 上的作用域深度選擇器如下所示,
<style scoped> .class1 >>> .class2 { /* ... */ } </style>
它將被轉(zhuǎn)換為,
.class1[data-v-f3f3eg9] .class2 { /* ... */ }
注意:如果您使用 SASS 等預(yù)處理器,則它可能無法正確處理 >>>。在這種情況下,使用 /deep/ 或 ::v-deep 組合器代替 >>> 組合器。
父樣式是否泄漏到作用域 css 中的子組件中?
父組件的樣式不會泄漏到子組件中。但是子組件的根節(jié)點(diǎn)會同時受到父級作用域 CSS 和子級作用域 CSS 的影響。即,您的子組件的根元素有一個類也存在于父組件中,父組件的樣式將泄漏給子組件。無論如何,這是設(shè)計使然,以便父級可以為子根元素設(shè)置樣式以用于布局目的。
例如,父組件的背景顏色屬性泄漏到子組件中,如下所示,
//parent.vue <template> ? <div class="wrapper"> ? ? <p>parent</p> ? ? <ChildMessageComponent/> ? </div> </template> <script> import ChildMessageComponent from "./components/child"; export default { ? name: "App", ? components: { ? ? ChildMessageComponent ? } }; </script> <style scoped> .wrapper { ? background: blue; } </style> //child.vue <template> ? <div class="wrapper"> ? ? <p>child</p> ? </div> </template> <script> export default { ? name: "Hello, Scoped CSS", }; </script> <style scoped> .wrapper { ? background: red; } </style>
現(xiàn)在子包裝器的背景顏色將變?yōu)樗{(lán)色而不是紅色。
如何在 vuejs 中使用 CSS 模塊?
以下是在 VueJS 中使用 css 模塊的步驟,
啟用 CSS 模塊: CSS 模塊必須通過將 modules: true 選項(xiàng)傳遞給 css-loader 來啟用
// webpack.config.js { ? module: { ? ? rules: [ ? ? ? // ... other rules omitted ? ? ? { ? ? ? ? test: /\.css$/, ? ? ? ? use: [ ? ? ? ? ? 'vue-style-loader', ? ? ? ? ? { ? ? ? ? ? ? loader: 'css-loader', ? ? ? ? ? ? options: { ? ? ? ? ? ? ? // enable CSS Modules ? ? ? ? ? ? ? modules: true, ? ? ? ? ? ? ? // customize generated class names ? ? ? ? ? ? ? localIdentName: '[local]_[hash:base64:8]' ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? ? ] ? ? ? } ? ? ] ? } }
添加模塊屬性:將模塊屬性添加到您的<style>
<style module> .customStyle { ? background: blue; } </style>
注入 CSS 模塊:您可以使用計算屬性 $style 注入 CSS 模塊對象
<template> ? <div :class="$style.blue"> ? ? Background color should be in blue ? </p> </template>
它可以使用 :class 綁定的對象/數(shù)組語法。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?cli3?項(xiàng)目中如何使用axios發(fā)送post請求
這篇文章主要介紹了vue?cli3?項(xiàng)目中如何使用axios發(fā)送post請求,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue.js與element-ui實(shí)現(xiàn)菜單樹形結(jié)構(gòu)的解決方法
本文通過實(shí)例給大家介紹了vue.js與element-ui實(shí)現(xiàn)菜單樹形結(jié)構(gòu),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-04-04vue-router項(xiàng)目實(shí)戰(zhàn)總結(jié)篇
vue-router 是 Vue.js 官方的路由庫.這篇文章主要介紹了vue-router項(xiàng)目實(shí)戰(zhàn)總結(jié),需要的朋友可以參考下2018-02-02Vue引入jquery實(shí)現(xiàn)平滑滾動到指定位置
這篇文章主要介紹了Vue引入jquery實(shí)現(xiàn)平滑滾動到指定位置的效果,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-05-05vue 表單驗(yàn)證按鈕事件交由父組件觸發(fā)的方法
這篇文章主要介紹了vue 表單驗(yàn)證按鈕事件交由父組件觸發(fā)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12