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);
});
// Another file, the below line is called bare import
import 'debug-keypress'
你不关心这里的任何导出;仅仅导入这个文件就应该设置按键记录,所以你只需要简单的导入。
关于javascript - ES6 裸导入 : How to use, 什么时候?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32773912/