ES6中export default与import语句的解构赋值是如何详细实现的?

更新于
2026-10-02 17:15:55
1阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1077个文字,预计阅读时间需要5分钟。

解构赋值+如下+config+对象+const+config+={+host:+'localhost',+port:+80+}+要+获取+其+中的+host+属性+let+{+host+}+=+config+成+模块+以上+两+段+代码+放在+同一+文件+中+不会+有问题+,但在+一个+项目中+,这样做+可能会导致+一些+问题+。

解构赋值

有如下 config 对象

const config = { host: 'localhost', port: 80 }

要获取其中的 host 属性

let { host } = config

拆分成模块

以上两段代码,放到同一个文件当中不会有什么问题,但在一个项目中,config 对象多处会用到,现在把 config 对象放到 config.js 文件当中。

// config.js export default { host: 'localhost', port: 80 }

在 app.js 中 import config.js

// app.js import config from './config' let { host } = config console.log(host) // => localhost console.log(config.host) // => localhost

上面这段代码也不会有问题。

阅读全文

本文共计1077个文字,预计阅读时间需要5分钟。

解构赋值+如下+config+对象+const+config+={+host:+'localhost',+port:+80+}+要+获取+其+中的+host+属性+let+{+host+}+=+config+成+模块+以上+两+段+代码+放在+同一+文件+中+不会+有问题+,但在+一个+项目中+,这样做+可能会导致+一些+问题+。

解构赋值

有如下 config 对象

const config = { host: 'localhost', port: 80 }

要获取其中的 host 属性

let { host } = config

拆分成模块

以上两段代码,放到同一个文件当中不会有什么问题,但在一个项目中,config 对象多处会用到,现在把 config 对象放到 config.js 文件当中。

// config.js export default { host: 'localhost', port: 80 }

在 app.js 中 import config.js

// app.js import config from './config' let { host } = config console.log(host) // => localhost console.log(config.host) // => localhost

上面这段代码也不会有问题。

阅读全文