ES6 shorthand 导入

ES6 shorthand import

是否有更短的 ES6 方法:

var assert = require('chai').assert;

import chai from 'chai';
var assert = chai.assert;

(chai 是 chai-assertion 库,以防你还没有听说过它。)

是的,你可以这样做:

import { assert } from 'chai';
在这种情况下,

assert 必须从 chai 导出。参见 spec here and about es6 modules here

你可以这样使用它:

import * as chai from 'chai';

let assert = chai.assert;
let should = chai.should();
let expect = chai.expect;

我宁愿:

import {assert, should, expect}  from 'chai';

我使用:

import chai from 'chai';
const { assert } = chai;