简体   繁体   English

如何用Javascript编写import语句而不是require语句?

[英]How to write import statement instead of require statement in Javascript?

I was importing one of the polyfill, "es6-promise" in my jsx file of react application but was not able to import it in a correct way. 我在我的React应用程序的jsx文件中导入了polyfill中的“ es6-promise”之一,但无法以正确的方式导入它。

I googled N number of solution finally one worked for IE. 我用谷歌搜索了N个解决方案,最后一个为IE工作。 require('es6-promise').polyfill()

its working fine but, why it was not working for import es6-promise from "es6-promise"; 它的工作正常,但是为什么不能import es6-promise from "es6-promise"; Is there any way to import it first in a variable and then calling the .polyfill() method to that variable? 有什么方法可以先将其导入变量中,然后再对该变量调用.polyfill()方法?

There a multiple ways of how you can import other modules in your code: import 有多种方法可以在代码中导入其他模块: import

For example: 例如:

import * as ES6Promise from 'es6-promise';

ES6Promise.polyfill();

Just a couple more ways you can do it. 您可以通过其他几种方式来做到这一点。

import { polyfill } from 'es6-promise';
polyfill();

or 要么

import ES6Promise from 'es6-promise';
ES6Promise.polyfill();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM