gpt4 book ai didi

javascript - 我如何将此 require 写为 es6 import 语句

转载 作者:搜寻专家 更新时间:2023-11-01 00:11:02 25 4
gpt4 key购买 nike

问题

我有这个要求声明

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

但我想把它写成 es6 import 语句

我试过了

import 'smoothscroll-polyfill';

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

但是无法让它正常工作,那么像这样导入包的正确方法是什么?

最佳答案

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

如果 polyfill 本身是一个命名的导出并且它不关心当它被调用时 this 是什么:

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

(您现在 confirmed 就是这种情况。)


为了完整起见,在确认以上内容之前,我还列出了这些可能对将来其他人有用的其他可能性:

如果 polyfilldefault 导出(而不是它自己命名的导出)的一个属性,那么我们导入默认值(没有{}import 语句中)然后使用它的属性:

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

如果 smoothScrollPolyFill 部分是一个命名导出并且 polyfill 是它的一个属性,那么我们将使用 { import 上:

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

关于javascript - 我如何将此 require 写为 es6 import 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42579853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com