我正在尝试只为IE11,https://react-hook-form.com/faqs/导入一个模块,然后在另一个文件中重新导入它。
// utils/index.js
let loadModule;
if (isIE11) {
loadModule = require('react-hook-form/dist/index.ie11');
} else {
loadModule = require('react-hook-form');
}
module.exports = { ...loadModule };
// import it in another file
import {useForm} from './utils/index';
...问题是这是否可以通过导入/导出来完成,以及我的代码是否是处理这种情况的正确方法
发布于 2020-11-05 07:06:02
问题是,这是否可以用
import/export实现,以及我的代码是否是处理这种情况的合适方法
不是import/export声明,不。但 (import(),ES2020的一部分)和top-level await (目前处于第3阶段,但很可能很快就会进入ES2021)也是可能的。
utils/index.js
const { useForm } = await import(
isIE11
? 'react-hook-form/dist/index.ie11'
: 'react-hook-form'
);
export { useForm };另一个文件:
import { useForm } from './utils/index';这两者在现代捆绑程序中都得到了很好的支持,而且由于您要处理的是IE11,所以如果您使用的是JavaScript模块而不是require,那么您必须要处理捆绑程序。
https://stackoverflow.com/questions/64688867
复制相似问题