Skip to content

Commit e0e6281

Browse files
AlienoverQC-L
andauthored
[Beta]: docs(cn): importing-and-exporting-components.md (#637)
Co-authored-by: QiChang Li <github@liqichang.com>
1 parent 67560c3 commit e0e6281

File tree

1 file changed

+72
-69
lines changed

1 file changed

+72
-69
lines changed

beta/src/pages/learn/importing-and-exporting-components.md

Lines changed: 72 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
---
2-
title: Importing and Exporting Components
2+
title: 组件的导入与导出
3+
translators:
4+
- Alienover
5+
- QC-L
36
---
47

58
<Intro>
69

7-
The magic of components lies in their reusability: you can create components that are composed of other components. But as you nest more and more components, it often makes sense to start splitting them into different files. This lets you keep your files easy to scan and reuse components in more places.
10+
组件的神奇之处在于它们的可重用性:你可以创建一个由其他组件构成的组件。但当你嵌套了越来越多的组件时,则需要将它们拆分成不同的文件。这样可以使得查找文件更加容易,并且能在更多地方复用这些组件。
811

912
</Intro>
1013

1114
<YouWillLearn>
1215

13-
* What a root component file is
14-
* How to import and export a component
15-
* When to use default and named imports and exports
16-
* How to import and export multiple components from one file
17-
* How to split components into multiple files
16+
* 何为根组件
17+
* 如何导入和导出一个组件
18+
* 何时使用默认和具名导入导出
19+
* 如何在一个文件中导入导出多个组件
20+
* 如何将组件拆分成多个文件
1821

1922
</YouWillLearn>
2023

21-
## The root component file {/*the-root-component-file*/}
24+
## 根组件文件 {/*the-root-component-file*/}
2225

23-
In [Your First Component](/learn/your-first-component), you made a `Profile` component and a `Gallery` component that renders it:
26+
[你的第一个组件](/learn/your-first-component) 中,你创建了一个 `Profile` 组件,并且渲染在 `Gallery` 组件里。
2427

2528
<Sandpack>
2629

@@ -37,7 +40,7 @@ function Profile() {
3740
export default function Gallery() {
3841
return (
3942
<section>
40-
<h1>Amazing scientists</h1>
43+
<h1>了不起的科学家们</h1>
4144
<Profile />
4245
<Profile />
4346
<Profile />
@@ -52,17 +55,17 @@ img { margin: 0 10px 10px 0; height: 90px; }
5255

5356
</Sandpack>
5457

55-
These currently live in a **root component file,** named `App.js` in this example. In [Create React App](https://create-react-app.dev/), your app lives in `src/App.js`. Depending on your setup, your root component could be in another file, though. If you use a framework with file-based routing, such as Next.js, your root component will be different for every page.
58+
在此示例中,所有组件目前都定义在**根组件** `App.js` 文件中,在 [Create React App](https://create-react-app.dev/) 中,你的应用应在 `src/App.js` 文件中定义。具体还需根据项目配置决定,有些根组件可能会声明在其他文件中。如果你使用的框架基于文件进行路由,如 Next.js,那你每个页面的根组件都会不一样。
5659

57-
## Exporting and importing a component {/*exporting-and-importing-a-component*/}
60+
## 导出和导入一个组件 {/*exporting-and-importing-a-component*/}
5861

59-
What if you want to change the landing screen in the future and put a list of science books there? Or place all the profiles somewhere else? It makes sense to move `Gallery` and `Profile` out of the root component file. This will make them more modular and reusable in other files. You can move a component in three steps:
62+
如果将来需要在首页添加关于科学书籍的列表,亦或者需要将所有的资料信息移动到其他文件。这时将 `Gallery` 组件和 `Profile` 组件移出根组件文件会更加合理。这会使组件更加模块化,并且可在其他文件中复用。你可以根据以下三个步骤对组件进行拆分:
6063

61-
1. **Make** a new JS file to put the components in.
62-
2. **Export** your function component from that file (using either [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_the_default_export) or [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_named_exports) exports).
63-
3. **Import** it in the file where you’ll use the component (using the corresponding technique for importing [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#importing_defaults) or [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#import_a_single_export_from_a_module) exports).
64+
1. **创建** 一个新的 JS 文件来存放该组件。
65+
2. **导出** 该文件中的函数组件(可以使用 [默认导出](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_the_default_export) [具名导出](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_named_exports)
66+
3. 在需要使用该组件的文件中 **导入**(可以根据相应的导出方式使用 [默认导入](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#importing_defaults) [具名导入](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#import_a_single_export_from_a_module))。
6467

65-
Here both `Profile` and `Gallery` have been moved out of `App.js` into a new file called `Gallery.js`. Now you can change `App.js` to import `Gallery` from `Gallery.js`:
68+
这里将 `Profile` 组件和 `Gallery` 组件,从 `App.js` 文件中移动到了 `Gallery.js` 文件中。修改后,即可在 `App.js` 中导入 `Gallery.js` 中的 `Gallery` 组件:
6669

6770
<Sandpack>
6871

@@ -89,7 +92,7 @@ function Profile() {
8992
export default function Gallery() {
9093
return (
9194
<section>
92-
<h1>Amazing scientists</h1>
95+
<h1>了不起的科学家们</h1>
9396
<Profile />
9497
<Profile />
9598
<Profile />
@@ -104,76 +107,76 @@ img { margin: 0 10px 10px 0; height: 90px; }
104107

105108
</Sandpack>
106109

107-
Notice how this example is broken down into two component files now:
110+
该示例中需要注意的是,如何将组件拆分成两个文件:
108111

109112
1. `Gallery.js`:
110-
- Defines the `Profile` component which is only used within the same file and is not exported.
111-
- Exports the `Gallery` component as a **default export**.
113+
- 定义了 `Profile` 组件,该组件仅在该文件内使用,没有被导出。
114+
- 使用 **默认导出** 的方式,将 `Gallery` 组件导出
112115
2. `App.js`:
113-
- Imports `Gallery` as a **default import** from `Gallery.js`.
114-
- Exports the root `App` component as a **default export**.
116+
- 使用 **默认导入** 的方式,从 `Gallery.js` 中导入 `Gallery` 组件。
117+
- 使用 **默认导出** 的方式,将根组件 `App` 导出。
115118

116119

117120
<Note>
118121

119-
You may encounter files that leave off the `.js` file extension like so:
122+
引入过程中,你可能会遇到一些文件并未添加 `.js` 文件后缀,如下所示:
120123

121124
```js
122125
import Gallery from './Gallery';
123126
```
124127

125-
Either `'./Gallery.js'` or `'./Gallery'` will work with React, though the former is closer to how [native ES Modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules) work.
128+
无论是 `'./Gallery.js'` 还是 `'./Gallery'`,在 React 里都能正常使用,只是前者更符合 [原生 ES 模块](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules)
126129

127130
</Note>
128131

129-
<DeepDive title="Default vs Named Exports">
132+
<DeepDive title="默认导出 vs 具名导出">
130133

131-
There are two primary ways to export values with JavaScript: default exports and named exports. So far, our examples have only used default exports. But you can use one or both of them in the same file. **A file can have no more than one _default_ export, but it can have as many _named_ exports as you like.**
134+
这是 JavaScript 里两个主要用来导出值的方式:默认导出和具名导出。到目前为止,我们的示例中只用到了默认导出。但你可以在一个文件中,选择使用其中一种,或者两种都使用。**一个文件里有且仅有一个 _默认_ 导出,但是可以有任意多个 _具名_ 导出。**
132135

133136
![Default and named exports](/images/docs/illustrations/i_import-export.svg)
134137

135-
How you export your component dictates how you must import it. You will get an error if you try to import a default export the same way you would a named export! This chart can help you keep track:
138+
组件的导出方式决定了其导入方式。当你用默认导入的方式,导入具名导出的组件时,就会报错。如下表格可以帮你更好地理解它们:
136139

137-
| Syntax | Export statement | Import statement |
138-
| ----------- | ----------- | ----------- |
139-
| Default | `export default function Button() {}` | `import Button from './button.js';` |
140-
| Named | `export function Button() {}` | `import { Button } from './button.js';` |
140+
| 语法 | 导出语句 | 导入语句 |
141+
| ----------- | ----------- | ----------- |
142+
| 默认 | `export default function Button() {}` | `import Button from './button.js';` |
143+
| 具名 | `export function Button() {}` | `import { Button } from './button.js';` |
141144

142-
When you write a _default_ import, you can put any name you want after `import`. For example, you could write `import Banana from './button.js'` instead and it would still provide you with the same default export. In contrast, with named imports, the name has to match on both sides. That's why they are called _named_ imports!
145+
当使用默认导入时,你可以在 `import` 语句后面进行任意命名。比如 `import Banana from './button.js'`,如此你能获得与默认导出一致的内容。相反,对于具名导入,导入和导出的名字必须一致。这也是为什么称其为 _具名_ 导入的原因!
143146

144-
**People often use default exports if the file exports only one component, and use named exports if it exports multiple components and values.** Regardless of which coding style you prefer, always give meaningful names to your component functions and the files that contain them. Components without names, like `export default () => {}`, are discouraged because they make debugging harder.
147+
**通常,文件中仅包含一个组件时,人们会选择默认导出,而当文件中包含多个组件或某个值需要导出时,则会选择具名导出。**无论选择哪种方式,请记得给你的组件和相应的文件命名一个有意义的名字。我们不建议创建未命名的组件,比如 `export default () => {}`,因为这样会使得调试变得异常困难。
145148

146149
</DeepDive>
147150

148-
## Exporting and importing multiple components from the same file {/*exporting-and-importing-multiple-components-from-the-same-file*/}
151+
## 从同一文件中导出和导入多个组件 {/*exporting-and-importing-multiple-components-from-the-same-file*/}
149152

150-
What if you want to show just one `Profile` instead of a gallery? You can export the `Profile` component, too. But `Gallery.js` already has a *default* export, and you can't have _two_ default exports. You could create a new file with a default export, or you could add a *named* export for `Profile`. **A file can only have one default export, but it can have numerous named exports!**
153+
如果你只想展示一个 `Profile` 组,而不展示整个图集。你也可以导出 `Profile` 组件。但 `Gallery.js` 中已包含 *默认* 导出,此时,你不能定义 _两个_ 默认导出。但你可以将其在新文件中进行默认导出,或者将 `Profile` 进行 *具名* 导出。**同一文件中,有且仅有一个默认导出,但可以有多个具名导出!**
151154

152-
> To reduce the potential confusion between default and named exports, some teams choose to only stick to one style (default or named), or avoid mixing them in a single file. It's a matter of preference. Do what works best for you!
155+
> 为了减少在默认导出和具名导出之间的混淆,一些团队会选择只使用一种风格(默认或者具名),或者禁止在单个文件内混合使用。这因人而异,选择最适合你的即可!
153156
154-
First, **export** `Profile` from `Gallery.js` using a named export (no `default` keyword):
157+
首先,用具名导出的方式,将 `Profile` 组件从 `Gallery.js` **导出**(不使用 `default` 关键字):
155158

156159
```js
157160
export function Profile() {
158161
// ...
159162
}
160163
```
161164

162-
Then, **import** `Profile` from `Gallery.js` to `App.js` using a named import (with the curly braces):
165+
接着,用具名导入的方式,从 `Gallery.js` 文件中 **导入** `Profile` 组件(用大括号):
163166

164167
```js
165168
import { Profile } from './Gallery.js';
166169
```
167170

168-
Finally, **render** `<Profile />` from the `App` component:
171+
最后,在 `App` 组件里 **渲染** `<Profile />`
169172

170173
```js
171174
export default function App() {
172175
return <Profile />;
173176
}
174177
```
175178

176-
Now `Gallery.js` contains two exports: a default `Gallery` export, and a named `Profile` export. `App.js` imports both of them. Try editing `<Profile />` to `<Gallery />` and back in this example:
179+
现在,`Gallery.js` 包含两个导出:一个是默认导出的 `Gallery`,另一个是具名导出的 `Profile``App.js` 中均导入了这两个组件。尝试将 `<Profile />` 改成 `<Gallery />`,回到示例中:
177180

178181
<Sandpack>
179182

@@ -201,7 +204,7 @@ export function Profile() {
201204
export default function Gallery() {
202205
return (
203206
<section>
204-
<h1>Amazing scientists</h1>
207+
<h1>了不起的科学家们</h1>
205208
<Profile />
206209
<Profile />
207210
<Profile />
@@ -216,47 +219,47 @@ img { margin: 0 10px 10px 0; height: 90px; }
216219

217220
</Sandpack>
218221

219-
Now you're using a mix of default and named exports:
222+
示例中混合使用了默认导出和具名导出:
220223

221224
* `Gallery.js`:
222-
- Exports the `Profile` component as a **named export called `Profile`**.
223-
- Exports the `Gallery` component as a **default export**.
225+
- 使用 **具名导出** 的方式,将 `Profile` 组件导出,并取名为 `Profile`
226+
- 使用 **默认导出** 的方式,将 `Gallery` 组件导出。
224227
* `App.js`:
225-
- Imports `Profile` as a **named import called `Profile`** from `Gallery.js`.
226-
- Imports `Gallery` as a **default import** from `Gallery.js`.
227-
- Exports the root `App` component as a **default export**.
228+
- 使用 **具名导入** 的方式,从 `Gallery.js` 中导入 `Profile` 组件,并取名为 `Profile`
229+
- 使用 **默认导入** 的方式,从 `Gallery.js` 中导入 `Gallery` 组件。
230+
- 使用 **默认导出** 的方式,将根组件 `App` 导出。
228231

229232
<Recap>
230233

231-
On this page you learned:
234+
在本章节中,你学到了:
232235

233-
* What a root component file is
234-
* How to import and export a component
235-
* When and how to use default and named imports and exports
236-
* How to export multiple components from the same file
236+
* 何为根组件
237+
* 如何导入和导出一个组件
238+
* 何时和如何使用默认和具名导入导出
239+
* 如何在一个文件里导出多个组件
237240

238241
</Recap>
239242

240243

241244

242245
<Challenges>
243246

244-
### Split the components further {/*split-the-components-further*/}
247+
### 进一步拆分组件 {/*split-the-components-further*/}
245248

246-
Currently, `Gallery.js` exports both `Profile` and `Gallery`, which is a bit confusing.
249+
现在,`Gallery.js` 同时导出了 `Profile` `Gallery`,这会让人感到有点混淆。
247250

248-
Move the `Profile` component to its own `Profile.js`, and then change the `App` component to render both `<Profile />` and `<Gallery />` one after another.
251+
尝试将 `Profile` 组件移动到 `Profile.js` 文件中,然后更新 `App` 组件,依次渲染 `<Profile />` `<Gallery />`
249252

250-
You may use either a default or a named export for `Profile`, but make sure that you use the corresponding import syntax in both `App.js` and `Gallery.js`! You can refer to the table from the deep dive above:
253+
你也许会使用默认导出或者具名导出的方式,来导出 `Profile` 组件,但请保证在 `App.js` `Gallery.js` 里使用相应的导入语句!具体可以参考下面的表格:
251254

252-
| Syntax | Export statement | Import statement |
253-
| ----------- | ----------- | ----------- |
254-
| Default | `export default function Button() {}` | `import Button from './button.js';` |
255-
| Named | `export function Button() {}` | `import { Button } from './button.js';` |
255+
| 语法 | 导出语句 | 导入语句 |
256+
| ----------- | ----------- | ----------- |
257+
| 默认 | `export default function Button() {}` | `import Button from './button.js';` |
258+
| 具名 | `export function Button() {}` | `import { Button } from './button.js';` |
256259

257260
<Hint>
258261

259-
Don't forget to import your components where they are called. Doesn't `Gallery` use `Profile`, too?
262+
不要忘记在调用它们的地方导入你的组件。因为 `Gallery` 中也调用了 `Profile` 组件。
260263

261264
</Hint>
262265

@@ -289,7 +292,7 @@ export function Profile() {
289292
export default function Gallery() {
290293
return (
291294
<section>
292-
<h1>Amazing scientists</h1>
295+
<h1>了不起的科学家们</h1>
293296
<Profile />
294297
<Profile />
295298
<Profile />
@@ -307,11 +310,11 @@ img { margin: 0 10px 10px 0; height: 90px; }
307310

308311
</Sandpack>
309312

310-
After you get it working with one kind of exports, make it work with the other kind.
313+
当你使用其中一种导出方式完成以上任务后,请尝试使用另一种导出方式实现。
311314

312315
<Solution>
313316

314-
This is the solution with named exports:
317+
具名导出的解决方案:
315318

316319
<Sandpack>
317320

@@ -335,7 +338,7 @@ import { Profile } from './Profile.js';
335338
export default function Gallery() {
336339
return (
337340
<section>
338-
<h1>Amazing scientists</h1>
341+
<h1>了不起的科学家们</h1>
339342
<Profile />
340343
<Profile />
341344
<Profile />
@@ -361,7 +364,7 @@ img { margin: 0 10px 10px 0; height: 90px; }
361364

362365
</Sandpack>
363366

364-
This is the solution with default exports:
367+
默认导出的解决方案:
365368

366369
<Sandpack>
367370

@@ -385,7 +388,7 @@ import Profile from './Profile.js';
385388
export default function Gallery() {
386389
return (
387390
<section>
388-
<h1>Amazing scientists</h1>
391+
<h1>了不起的科学家们</h1>
389392
<Profile />
390393
<Profile />
391394
<Profile />
@@ -413,4 +416,4 @@ img { margin: 0 10px 10px 0; height: 90px; }
413416

414417
</Solution>
415418

416-
</Challenges>
419+
</Challenges>

0 commit comments

Comments
 (0)