ES6 裸导入:如何使用,何时使用?
ES6 Bare Import: How to use, and when?
ES6 允许我们使用新的导入语法。使用它,我们可以将模块或这些模块的一部分导入到我们的代码中。使用示例包括:
// Import the default export from a module.
import React from 'react';
// Import named exports from a module.
import { Component, PropTypes } from 'react';
// Named import - grab everything from the module and assign it to "redux".
import * as Redux from 'react-redux';
但是,我们也有这个谜团:
import 'react';
看起来 ES6 似乎支持 裸导入,因为这是一个有效的导入语句。但是,如果这样做,似乎没有办法实际引用该模块。
我们将如何使用它,为什么?
对于副作用。例如(未经测试,仅概念):
// debug-keypresses.js
document.addEventListener('keypress', evt => {
console.log("KEYPRESS:", evt.which);
});
你不关心这里的任何导出;仅仅导入这个文件就应该设置按键记录,所以你只需要简单的导入。
ES6 允许我们使用新的导入语法。使用它,我们可以将模块或这些模块的一部分导入到我们的代码中。使用示例包括:
// Import the default export from a module.
import React from 'react';
// Import named exports from a module.
import { Component, PropTypes } from 'react';
// Named import - grab everything from the module and assign it to "redux".
import * as Redux from 'react-redux';
但是,我们也有这个谜团:
import 'react';
看起来 ES6 似乎支持 裸导入,因为这是一个有效的导入语句。但是,如果这样做,似乎没有办法实际引用该模块。
我们将如何使用它,为什么?
对于副作用。例如(未经测试,仅概念):
// debug-keypresses.js
document.addEventListener('keypress', evt => {
console.log("KEYPRESS:", evt.which);
});
你不关心这里的任何导出;仅仅导入这个文件就应该设置按键记录,所以你只需要简单的导入。