如何在 ES6 模块中导入部分对象 [英] How to import part of object in ES6 modules

查看:28
本文介绍了如何在 ES6 模块中导入部分对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

react 文档中,我找到了这种导入方式纯渲染混合

In the react documentation I found this way to import PureRenderMixin

var PureRenderMixin = require('react/addons').addons.PureRenderMixin;

如何改写成 ES6 风格.我唯一能做的就是:

How can it be rewritten in ES6 style. The only thing I can do is:

import addons from "react/addons";
let PureRenderMixin = addons.addons.PureRenderMixin;

我希望有更好的方法.

推荐答案

很遗憾 import 语句不像对象解构那样工作.这里的花括号意味着您要导入具有此名称的令牌,但不是默认导出的属性.看看这对导入/导出:

Unfortunately import statements does not work like object destructuring. Curly braces here mean that you want to import token with this name but not property of default export. Look at this pairs of import/export:

 //module.js
 export default 'A';
 export var B = 'B';

 //script.js
 import A from './a.js';  //import value on default export
 import {B} from './a.js'; // import value by its name
 console.log(A, B); // 'A', 'B'

对于您的情况,您可以导入整个对象并进行解构赋值

For your case you can import whole object and make a destructuring assignment

 import addons from "react/addons";
 let {addons: {PureRenderMixin}} = addons;

这篇关于如何在 ES6 模块中导入部分对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆