Home >Web Front-end >Vue.js >Vue3 learning in-depth analysis of CSS Modules and Scope
Css Modules is to add the label class name into a unique class name, for example, .class is converted into .class_abc_123, similar to symbol, unique key name
Css Scope achieves scope isolation by adding a custom attribute to an element and adding a unique number to this attribute.
Class name naming rules generate a unique name for the class to achieve scope isolation. [Related recommendations: vuejs video tutorial, web front-end development]
<style module> .title { font-size: 14px; font-family: Microsoft YaHei, Microsoft YaHei-Bold; font-weight: 700; color: #13161b; } .name { display: flex; align-items: center; &-img { width: 24px; height: 24px; border-radius: 4px; } &-text { font-size: 14px; font-family: Microsoft YaHei, Microsoft YaHei-Regular; font-weight: 400; color: #13161b; } } </style>
cell: (h, { col, row }) => { // console.log(style); return ( <span class={style.name}> <img src={testImage} class={style['name-img']} /> <span class={style['name-text']}>{row.name}</span> </span> ); },
<template> <div class="example">hi</div> </template> <style module> .example { color: red; } </style>
<!-- 用自定义属性把类名封装起来了 --> <style> .example[data-v-f3f3eg9] { color: red; } </style> <template> <div class="example" data-v-f3f3eg9>hi</div> </template>
c9ccee2e6ea535a969eb3f532ad9fe89 Add the module attribute, that is,
a6167d77ee734aaef6dcd2aa69de7b33.
a6167d77ee734aaef6dcd2aa69de7b33 The code block will be compiled into CSS Modules and the generated CSS class will be exposed to the component as the key of the
$style object, which can be used directly in the template
$style. For named CSS Modules such as
8784e2fd63a27a4a8fec0995a8e86d19, the compiled CSS class is exposed to the component as the key of the
content object, that is,
module Whatever the attribute value is, the object will be exposed.
<script setup> import { useCssModule } from 'vue' // 不传递参数,获取<style module>代码块编译后的css类对象 const style = useCssModule() console.log(style.success) // 获取到的是success类名经过 hash 计算后的类名 // 传递参数content,获取<style module="content">代码块编译后的css类对象 const contentStyle = useCssModule('content') </script> <template> <div>普通style red</div> <div :class="$style.success">默认CssModule pink</div> <div :class="style.success">默认CssModule pink</div> <div :class="contentStyle.success">具名CssModule blue</div> <div :class="content.success">具名CssModule blue</div> </template> <!-- 普通style --> <style> .success { color: red; } </style> <!-- 无值的css module --> <style module> .success { color: pink; } </style> <!-- 具名的css module --> <style module="content"> .success { color: blue; } </style>
Note that for CSS Modules with the same name, the later ones will overwrite the previous ones.
For module naming distinction, it is mainly used in JSX and TSX componentscell: (h, { col, row }) => { // console.log(style); return ( <span class={style.name}> <img src={testImage} class={style['name-img']} /> <span class={style['name-text']}>{row.name}</span> </span> ); },For example, the render function
<script> export default { props: { text: { type: String, default: '' } }, render(h) { return <span class={this.$style.span1}>hello 222 - {this.text}</span>; } }; </script> <style module> .span1 { color: blue; font-size: 40px; } </style>
But we need to Note that it is best to have a class encapsulation outside, otherwise it may affect the global style.
/* 转化前 */ <style scoped> .a :deep(.b) { /* ... */ } </style> /* 转化后 */ .a[data-v-f3f3eg9] .b { /* ... */ }In actual projects, when we want to modify the default style of the component library used, when using the Scoped CSS solution, we can modify its default style through the depth selector. Several ways to write depth left and right selectors:
when using the Sass preprocessor. However, in Vue3, improvements have been made as follows:
:deep(.child-class) instead::v-deep
CSS Modules | Scoped CSS |
---|---|
Vue Loader supports it by default, no additional configuration is required | |
By customizing the hash attribute for the element, and then using the attribute selector to select the element to achieve scope isolation | |
and declare scoped | |
/ | |
1. You can define global styles so that the styles are not restricted by the scope; 2. You can select through deep action The tool hits the sub-component to control the style of the sub-component |
vuejs introductory tutorial, Basic programming video )
The above is the detailed content of Vue3 learning in-depth analysis of CSS Modules and Scope. For more information, please follow other related articles on the PHP Chinese website!