ES6 Bare Import:如何使用以及何时使用? [英] ES6 Bare Import: How to use, and when?

查看:353
本文介绍了ES6 Bare Import:如何使用以及何时使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ES6允许我们使用新的导入语法.使用它,我们可以将模块导入到我们的代码或这些模块的一部分中.用法示例包括:

ES6 allows us to use a new import syntax. Using it, we can import modules into our code, or parts of those modules. Examples of usage include:

import React from 'react'; // Import the default export from a module.
import { Component, PropTypes } from 'react'; // Import named exports from a module.
import * as Redux from 'react-redux'; // Named import - grab everything from the module and assign it to "redux".

但随后,我们也有一个谜:

But then, we also have this mystery:

import 'react';

ES6似乎支持裸导入",因为这是有效的导入语句.但是,如果这样做,似乎似乎没有办法实际引用该模块.

It looks as though ES6 supports a 'bare import', as this is a valid import statement. However, if this is done, it seems as though there's no way to actually reference the module.

我们将如何使用它,为什么?

How would we use this, and why?

推荐答案

用于副作用.例如(未经测试,仅概念):

For side-effects. For example (untested, concept-only):

// debug-keypresses.js

import $ from 'jquery';

$(document).keypress(evt => {
  console.log("KEYPRESS:", evt.which);
});

您不在乎这里的任何出口;仅导入该文件就应该设置按键的记录,因此只需导入就可以了.

You don't care about any exports here; the mere importing of this file should set up logging of keypresses, so bare import is all you need.

确实,正如loganfsmyth在评论中指出的那样,这是import 'debug-keypresses'的示例,而不是import $ from 'jquery'的示例.

Indeed, as noted by loganfsmyth in comments, this is an example for import 'debug-keypresses', not for import $ from 'jquery'.

这篇关于ES6 Bare Import:如何使用以及何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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