You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: src/api/sfc-style.md
+31-32Lines changed: 31 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,11 @@
2
2
sidebarDepth: 1
3
3
---
4
4
5
-
<!-- TODO: translation -->
6
-
# SFC Style Features
5
+
# 单文件组件样式特性
7
6
8
7
## `<style scoped>`
9
8
10
-
When a `<style>`tag has the `scoped` attribute, its CSS will apply to elements of the current component only. This is similar to the style encapsulation found in Shadow DOM. It comes with some caveats, but doesn't require any polyfills. It is achieved by using PostCSS to transform the following:
@@ -21,7 +20,7 @@ When a `<style>` tag has the `scoped` attribute, its CSS will apply to elements
21
20
</template>
22
21
```
23
22
24
-
Into the following:
23
+
转换为:
25
24
26
25
```vue
27
26
<style>
@@ -35,13 +34,13 @@ Into the following:
35
34
</template>
36
35
```
37
36
38
-
### Child Component Root Elements
37
+
### 子组件的根元素
39
38
40
-
With`scoped`, the parent component's styles will not leak into child components. However, a child component's root node will be affected by both the parent's scoped CSS and the child's scoped CSS. This is by design so that the parent can style the child root element for layout purposes.
@@ -51,7 +50,7 @@ If you want a selector in `scoped` styles to be "deep", i.e. affecting child com
51
50
</style>
52
51
```
53
52
54
-
The above will be compiled into:
53
+
上面的代码会被编译成:
55
54
56
55
```css
57
56
.a[data-v-f3f3eg9] .b {
@@ -60,12 +59,12 @@ The above will be compiled into:
60
59
```
61
60
62
61
:::tip
63
-
DOM content created with `v-html`are not affected by scoped styles, but you can still style them using deep selectors.
62
+
通过 `v-html`创建的 DOM 内容不会被作用域样式影响,但你仍然可以使用深度选择器来设置其样式。
64
63
:::
65
64
66
-
### Slotted Selectors
65
+
### 插槽选择器
67
66
68
-
By default, scoped styles do not affect contents rendered by `<slot/>`, as they are considered to be owned by the parent component passing them in. To explicitly target slot content, use the `:slotted`pseudo-class:
@@ -87,9 +86,9 @@ If you want just one rule to apply globally, you can use the `:global` pseudo-cl
87
86
</style>
88
87
```
89
88
90
-
### Mixing Local and Global Styles
89
+
### 混合使用局部与全局样式
91
90
92
-
You can also include both scoped and non-scoped styles in the same component:
91
+
你也可以在同一个组件中同时包含作用域样式和非作用域样式:
93
92
94
93
```vue
95
94
<style>
@@ -101,15 +100,15 @@ You can also include both scoped and non-scoped styles in the same component:
101
100
</style>
102
101
```
103
102
104
-
### Scoped Style Tips
103
+
### 作用域样式提示
105
104
106
-
-**Scoped styles do not eliminate the need for classes**. Due to the way browsers render various CSS selectors, `p { color: red }`will be many times slower when scoped (i.e. when combined with an attribute selector). If you use classes or ids instead, such as in `.example { color: red }`, then you virtually eliminate that performance hit.
105
+
-**作用域样式并没有消除对 CSS 类的需求**。由于浏览器渲染各种各样 CSS 选择器的方式,`p { color: red }`结合作用域样式使用时会慢很多倍 (即,当与 attribute 选择器组合使用的时候)。如果使用 class 或者 id 来替代,例如 `.example { color: red }`,那你几乎就可以避免这个性能的损失。
107
106
108
-
-**Be careful with descendant selectors in recursive components!** For a CSS rule with the selector `.a .b`, if the element that matches `.a`contains a recursive child component, then all `.b`in that child component will be matched by the rule.
A `<style module>`tag is compiled as [CSS Modules](https://github.com/css-modules/css-modules)and exposes the resulting CSS classes to the component as an object under the key of `$style`:
Refer to the [CSS Modules spec](https://github.com/css-modules/css-modules) for mode details such as [global exceptions](https://github.com/css-modules/css-modules#exceptions)and[composition](https://github.com/css-modules/css-modules#composition).
@@ -145,21 +144,21 @@ You can customize the property key of the injected classes object by giving the
145
144
</style>
146
145
```
147
146
148
-
### Usage with Composition API
147
+
### 与组合式 API 一同使用
149
148
150
-
The injected classes can be accessed in `setup()` and `<script setup>` via the [`useCssModule`](/api/global-api.html#usecssmodule) API. For `<style module>`blocks with custom injection names, `useCssModule`accepts the matching `module` attribute value as the first argument:
The actual value will be compiled into a hashed CSS custom property, so the CSS is still static. The custom property will be applied to the component's root element via inline styles and reactively updated if the source value changes.
0 commit comments