我想在我的 React 应用程序中实现延迟加载,但大多数文件都是 JSON。当视口宽度小于例如 768px 时,它们中的一些正在加载,并且我不想在不需要它们时将它们加载到桌面应用程序中。有没有办法用 React.lazy() 加载 JSON 文件?
我的 JSON 文件是使用 Adobe After Effect 扩展生成的,名为:“Bodymovin”,后来我像这样导入这个文件:
import * as background from './background.json';
import * as backgroundLarge from './background-large.json';
import * as backgroundMedium from './background-medium.json';
import * as backgroundMobile from './background-mobile.json';
import * as backgroundSmallMobile from './background-smallMobile.json';
import * as tree from './tree.json';
import * as treeLarge from './tree-large.json';
import * as treeMedium from './tree-medium.json';
import * as treeMobile from './tree-mobile.json';
import * as treeSmallMobile from './tree-smallMobile.json';
我试图像使用默认导出的任何其他组件一样正常加载它,但它不起作用。
const treeMedium = lazy(() => import('./tree-medium.json'));
当我正常导入 JSON 时,我稍后会像这样使用它:
backgroundOptions: {
loop: true,
autoplay: true,
animationData: background.default,
},
并将这个对象Lottie
从react-lottie
我可以将其转换为延迟加载还是我想错了?