ES6中export default与import解构赋值如何实现?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1074个文字,预计阅读时间需要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
上面这段代码也不会有问题。
本文共计1074个文字,预计阅读时间需要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
上面这段代码也不会有问题。

