2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7b8d367417b2b2512b59
|
|
|
|
challengeType: 1
|
2020-08-04 15:13:35 +08:00
|
|
|
forumTopicId: 301205
|
|
|
|
localeTitle: 导入一个默认的导出
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-08-04 15:13:35 +08:00
|
|
|
<section id='description'>
|
|
|
|
在上一个挑战里,学习了<code>export default</code>的用法。还需要一种<code>import</code>的语法来导入默认的导出。
|
|
|
|
在下面的例子里有一个<code>add</code>函数, 它在<code>"math_functions"</code>文件里默认被导出。来看看来如何导入它:
|
|
|
|
|
|
|
|
```js
|
|
|
|
import add from "./math_functions.js";
|
|
|
|
```
|
|
|
|
|
|
|
|
这个语法只有一处不同的地方 —— 被导入的<code>add</code>值,并没有被花括号<code>{}</code>所包围。与导出值的方法不同,导入默认导出的写法仅仅只是简单的将变量名写在<code>import</code>之后。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:13:35 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
在下面的代码中,请导入在同目录下的<code>"math_functions"</code>文件中默认导出的<code>subtract</code>值。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-08-04 15:13:35 +08:00
|
|
|
- text: 正确导入<code>export default</code>方法导出的值。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(code.match(/import\s+subtract\s+from\s+('|")\.\/math_functions\.js\1/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:13:35 +08:00
|
|
|
|
|
|
|
// add code above this line
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-08-04 15:13:35 +08:00
|
|
|
subtract(7,4);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:13:35 +08:00
|
|
|
import subtract from "./math_functions.js";
|
|
|
|
// add code above this line
|
|
|
|
|
|
|
|
subtract(7,4);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-04 15:13:35 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|