ES6 - Convert from 'require' to 'import'

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

If the alternative to:

 var Foo = require('foo'); 

is:

 import Foo from 'foo'; 

What is the alternative to:

var Bar = require('foo').batz 

Could it be:

import {batz}  from 'foo' ? 

回答1:

Nearly. It does however depend on how you are exporting them.

  • named exports (export var batz = …):

    import {batz as Bar} from 'foo'; 
  • default-exported object (export default {batz: …};) - should not be used:

    import Foo from 'foo'; var Bar = Foo.batz; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!