6. 完成以下需求(es6 module) ```js // a.js const name = "John"; const age = 30; const city = "New York"; // 单个导出数据 ``` ```js // b.js const name2 = "tom"; const age2 = 11; const city2 = "sina"; // 默认导出数据 ``` ```js // c.js // 导入a.js 使用里面的数据 // 导入b.js 使用里面的数据 ```
时间: 2023-11-21 09:42:53 浏览: 39
你可以使用 ES6 模块导出和导入的语法来完成这个需求:
```js
// a.js
export const name = "John";
export const age = 30;
export const city = "New York";
```
```js
// b.js
const name2 = "tom";
const age2 = 11;
const city2 = "sina";
export default { name2, age2, city2 };
```
```js
// c.js
import { name, age, city } from "./a.js";
import data from "./b.js";
console.log(name); // 输出: "John"
console.log(age); // 输出: 30
console.log(city); // 输出: "New York"
console.log(data.name2); // 输出: "tom"
console.log(data.age2); // 输出: 11
console.log(data.city2); // 输出: "sina"
```
在 `a.js` 中,我们使用 `export` 关键字将 `name`、`age` 和 `city` 导出为单个导出。在 `b.js` 中,我们使用 `export default` 关键字将一个对象包含的数据作为默认导出。在 `c.js` 中,我们使用 `import` 关键字来导入 `a.js` 和 `b.js` 中的数据。然后我们可以直接使用这些导入的数据进行操作。