How do changes require import with a key in ES6?

I want to write a query with ES6 imports. In the keyless case, this is pretty easy to do

var args2 = require('yargs2');

import foo from 'bar';

But with a key, I cannot find the corresponding syntax:

var foo = require('bar').key;

      

How can i do this?

+3


source to share


2 answers


The syntax for importing an aliased module member is:

import {key as foo} from 'bar';

      

This is equivalent to var foo = require('bar').key;

If you want to import an element without overlaying it, the syntax is simpler:



import {foo} from 'bar';

      

Equivalent to:

 var foo = require('bar').foo;

      

MDN article on import approval

+6


source


var foo = require('bar').key

identical var bar = require('bar'); var foo = bar.key

(otherwise than declaring the variable "bar", which is probably no longer needed).

if you export an object named "key" it will be the same in ES6 import / export.



import bar from 'bar';
var foo = bar.key;

      

Note This assumes a default export ( export default xxx

) as in the OP. If named export ( export foo

) is used the syntax to use is -import {foo} from 'bar'

+3


source







All Articles