【发布时间】:2021-11-29 16:46:17
【问题描述】:
我尝试在 React / Next.js 项目中进行代码拆分,但遇到了障碍。
我正在关注这里的操作方法:https://reactjs.org/docs/code-splitting.html
在此处导入:
const { SHA512 } = React.lazy(() => import("crypto-js"));
并在这里使用useEffect:
useEffect(() => {
setTimeout(() => {
let window2: any = window;
if (window2.bp && process.env.NEXT_PUBLIC_BARION_ID) {
const { totalPriceInt, unitPriceInt } = calcUnitAndTotalPrice(
startPaymentIn,
buyTicketData
);
window2.bp("track", "initiateCheckout", {
contentType: "Product",
currency: "HUF",
id: startPaymentIn.eventId,
name: buyTicketData?.name,
quantity: startPaymentIn.quantity ?? 1.0,
unit: "db",
imageUrl: `https://ticket-t01.s3.eu-central-1.amazonaws.com/${buyTicketData?.imgId}_0.cover.jpg`,
list: "ProductPage",
});
window2.bp("track", "setUserProperties", {
userId: SHA512(getTempUserId(localStorage)).toString(), // <--- HERE
});
}
}, 4000);
}, []);
但得到这个错误:
./components/LoginAndRegistration.tsx:25:9
Type error: Property 'SHA512' does not exist on type 'ExoticComponent<any> & { readonly _result: ComponentType<any>; }'.
23 | import { GoogleLogin } from "react-google-login";
24 | import axios from "axios";
> 25 | const { SHA512 } = React.lazy(() => import("crypto-js"));
| ^
26 |
27 | interface LoginAndRegistrationProps {
28 | isRegistration: boolean;
error Command failed with exit code 1.
你知道为什么会出错吗?
在没有延迟加载的情况下工作之前:
import { SHA512 } from "crypto-js";
我也尝试了 Next.js 解决方法:
import dynamic from "next/dynamic";
const { SHA512 } = dynamic(() => import("crypto-js"));
但出现此错误:
Type error: 'await' expressions are only allowed within async functions and at the top levels of modules.
130 |
131 | useEffect(() => {
> 132 | const { SHA512 } = await import("crypto-js");
| ^
133 | setTimeout(() => {
134 | let window2: any = window;
135 | if (window2.bp && process.env.NEXT_PUBLIC_BARION_ID) {
【问题讨论】:
标签: reactjs next.js code-splitting