本文介绍了ES6 - 从'require'转换为'import'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果替代:

  var Foo = require('foo'); 

是:



从$ foo导入Foo;

替代方法:

  var Bar = require('foo')。batz 

可以是:

  import {batz} from'foo'? 


解决方案

几乎。但是它取决于您如何导出它们。




  • 命名exports( export var batz = ... ):

      import {batz as Bar} from'foo' 


  • 默认导出的对象( export default {batz:...} ; ) - 不应该使用:

     从foo导入Foo; 
    var Bar = Foo.batz;



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' ?
解决方案

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;
    

这篇关于ES6 - 从'require'转换为'import'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 22:57