我如何写这个要求作为一个es6导入语句 [英] How do i write this require as an es6 import statement

查看:171
本文介绍了我如何写这个要求作为一个es6导入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



我有这个需求声明

  require 'smoothscroll-填充工具')填充工具(); 

但是我想把它写成一个es6 import语句



我尝试过

  import'smoothscroll-polyfill'; 

  import * as'smoothscrollPolyfill'from'smoothscroll-polyfill'; 

但是无法使其正常工作,那么导入包的方法是什么?

解决方案

您可以分两部分进行,首先是导入,然后调用函数:

$ b $从'smoothscroll-polyfill'导入smoothScrollPolyFill; b

  
const polyfill = smoothScrollPolyFill.polyfill();

假设它是默认导出。如果它是一个命名导出,它将是:

  import'smoothscrollPolyFill'from'smoothscroll-polyfill'; 
const polyfill = smoothScrollPolyFill.polyfill();

另外,如果 polyfill 本身是一个命名导出,它不在乎什么这个是在调用时:



<$ p $来自'smoothscroll-polyfill'的p> import {polyfill};
polyfill();

...但再次注意假设。


Problem

I have this require statement

require('smoothscroll-polyfill').polyfill();

But I would like to write it as an es6 import statement

I have tried

import 'smoothscroll-polyfill';

and

import * as 'smoothscrollPolyfill' from 'smoothscroll-polyfill';

But cant get it to work correctly, so what is the correct way to import a package like this?

解决方案

You'd do it in two parts, first the import, then the function call:

import smoothScrollPolyFill from 'smoothscroll-polyfill';
const polyfill = smoothScrollPolyFill.polyfill();

That assumes it's the default export. If it's a named export, it would be:

import {smoothScrollPolyFill} from 'smoothscroll-polyfill';
const polyfill = smoothScrollPolyFill.polyfill();

Alternately, if polyfill itself is a named export and it doesn't care what this is when it's called:

import {polyfill} from 'smoothscroll-polyfill';
polyfill();

...but again note the assumptions.

这篇关于我如何写这个要求作为一个es6导入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆