Skip to content
This repository was archived by the owner on Aug 8, 2022. It is now read-only.

Commit f3ea05e

Browse files
arronKlerzouyawei.fewxsmsveaba
authored
docs: translate sfc-style.md (#653)
* docs: translate sfc-style.md * Apply suggestions from code review Co-authored-by: wxsm <[email protected]> * fix: reserver property and attribute in translation * Apply suggestions from code review Co-authored-by: Godpu <[email protected]> * Update src/api/sfc-style.md Co-authored-by: Arron Zou <[email protected]> Co-authored-by: zouyawei.fe <[email protected]> Co-authored-by: wxsm <[email protected]> Co-authored-by: Godpu <[email protected]>
1 parent ed61d6a commit f3ea05e

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/api/sfc-style.md

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
sidebarDepth: 1
33
---
44

5-
<!-- TODO: translation -->
6-
# SFC Style Features
5+
# 单文件组件样式特性
76

87
## `<style scoped>`
98

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:
9+
`<style>` 标签带有 `scoped` attribute 的时候,它的 CSS 只会应用到当前组件的元素上。这类似于 Shadow DOM 中的样式封装。它带有一些注意事项,不过好处是不需要任何的 polyfill。它是通过 PostCSS 转换以下内容来实现的:
1110

1211
```vue
1312
<style scoped>
@@ -21,7 +20,7 @@ When a `<style>` tag has the `scoped` attribute, its CSS will apply to elements
2120
</template>
2221
```
2322

24-
Into the following:
23+
转换为:
2524

2625
```vue
2726
<style>
@@ -35,13 +34,13 @@ Into the following:
3534
</template>
3635
```
3736

38-
### Child Component Root Elements
37+
### 子组件的根元素
3938

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.
39+
在带有 `scoped` 的时候,父组件的样式将不会泄露到子组件当中。不过,子组件的根节点会同时被父组件的作用域样式和子组件的作用域样式影响。这是有意为之的,这样父组件就可以设置子组件根节点的样式,以达到调整布局的目的。
4140

42-
### Deep Selectors
41+
### 深度选择器
4342

44-
If you want a selector in `scoped` styles to be "deep", i.e. affecting child components, you can use the `:deep()` pseudo-class:
43+
处于 `scoped` 样式中的选择器如果想要做更“深度”的选择,也即:影响到子组件,可以使用 `:deep()` 这个伪类:
4544

4645
```vue
4746
<style scoped>
@@ -51,7 +50,7 @@ If you want a selector in `scoped` styles to be "deep", i.e. affecting child com
5150
</style>
5251
```
5352

54-
The above will be compiled into:
53+
上面的代码会被编译成:
5554

5655
```css
5756
.a[data-v-f3f3eg9] .b {
@@ -60,12 +59,12 @@ The above will be compiled into:
6059
```
6160

6261
:::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 内容不会被作用域样式影响,但你仍然可以使用深度选择器来设置其样式。
6463
:::
6564

66-
### Slotted Selectors
65+
### 插槽选择器
6766

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:
67+
默认情况下,作用域样式不会影响到 `<slot/>` 渲染出来的内容,因为它们被认为是父组件所持有并传递进来的。使用 `:slotted` 伪类以确切地将插槽内容作为选择器的目标:
6968

7069
```vue
7170
<style scoped>
@@ -75,9 +74,9 @@ By default, scoped styles do not affect contents rendered by `<slot/>`, as they
7574
</style>
7675
```
7776

78-
### Global Selectors
77+
### 全局选择器
7978

80-
If you want just one rule to apply globally, you can use the `:global` pseudo-class rather than creating another `<style>` (see below):
79+
如果想让其中一个样式规则应用到全局,比起另外创建一个 `<style>`,可以使用 `:global` 伪类来实现 (看下面的代码):
8180

8281
```vue
8382
<style scoped>
@@ -87,9 +86,9 @@ If you want just one rule to apply globally, you can use the `:global` pseudo-cl
8786
</style>
8887
```
8988

90-
### Mixing Local and Global Styles
89+
### 混合使用局部与全局样式
9190

92-
You can also include both scoped and non-scoped styles in the same component:
91+
你也可以在同一个组件中同时包含作用域样式和非作用域样式:
9392

9493
```vue
9594
<style>
@@ -101,15 +100,15 @@ You can also include both scoped and non-scoped styles in the same component:
101100
</style>
102101
```
103102

104-
### Scoped Style Tips
103+
### 作用域样式提示
105104

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 }`,那你几乎就可以避免这个性能的损失。
107106

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.
107+
- **小心递归组件中的后代选择器**。对于一个使用了 `.a .b` 选择器的样式规则来说,如果匹配到 `.a` 的元素包含了一个递归的子组件,那么所有的在那个子组件中的 `.b` 都会匹配到这条样式规则。
109108

110109
## `<style module>`
111110

112-
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`:
111+
`<style module>` 标签会被编译为 [CSS Modules](https://github.com/css-modules/css-modules) 并且将生成的 CSS 类作为 `$style` 对象的键暴露给组件:
113112

114113
```vue
115114
<template>
@@ -125,13 +124,13 @@ A `<style module>` tag is compiled as [CSS Modules](https://github.com/css-modul
125124
</style>
126125
```
127126

128-
The resulting classes are hashed to avoid collision, achieving the same effect of scoping the CSS to the current component only.
127+
对生成的类做 hash 计算以避免冲突,实现了和 scope CSS 一样将 CSS 仅作用于当前组件的效果。
129128

130-
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).
129+
参考 [CSS Modules 规范](https://github.com/css-modules/css-modules)以查看更多详情,例如 [global exceptions](https://github.com/css-modules/css-modules#exceptions) [composition](https://github.com/css-modules/css-modules#composition)
131130

132-
### Custom Inject Name
131+
### 自定义注入名称
133132

134-
You can customize the property key of the injected classes object by giving the `module` attribute a value:
133+
你可以通过给 `module` attribute 一个值来自定义注入的类对象的 property 键:
135134

136135
```vue
137136
<template>
@@ -145,21 +144,21 @@ You can customize the property key of the injected classes object by giving the
145144
</style>
146145
```
147146

148-
### Usage with Composition API
147+
### 与组合式 API 一同使用
149148

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:
149+
注入的类可以通过 [`useCssModule`](/api/global-api.html#usecssmodule) API`setup()``<script setup>` 中使用。对于使用了自定义注入名称的 `<style module>` 模块,`useCssModule` 接收一个对应的 `module` attribute 值作为第一个参数。
151150

152151
```js
153-
// default, returns classes for <style module>
152+
// 默认, 返回 <style module> 中的类
154153
useCssModule()
155154

156-
// named, returns classes for <style module="classes">
155+
// 命名, 返回 <style module="classes"> 中的类
157156
useCssModule('classes')
158157
```
159158

160-
## State-Driven Dynamic CSS
159+
## 状态驱动的动态 CSS
161160

162-
SFC `<style>` tags support linking CSS values to dynamic component state using the `v-bind` CSS function:
161+
单文件组件的 `<style>` 标签可以通过 `v-bind` 这一 CSS 函数将 CSS 的值关联到动态的组件状态上:
163162

164163
```vue
165164
<template>
@@ -183,7 +182,7 @@ export default {
183182
</style>
184183
```
185184

186-
The syntax works with [`<script setup>`](./sfc-script-setup), and supports JavaScript expressions (must be wrapped in quotes):
185+
这个语法同样也适用于 [`<script setup>`](./sfc-script-setup),且支持 JavaScript 表达式 (需要用引号包裹起来)
187186

188187
```vue
189188
<script setup>
@@ -203,4 +202,4 @@ p {
203202
</style>
204203
```
205204

206-
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.
205+
实际的值会被编译成 hash 的 CSS 自定义 propertyCSS 本身仍然是静态的。自定义 property 会通过内联样式的方式应用到组件的根元素上,并且在源值变更的时候响应式更新。

0 commit comments

Comments
 (0)