Rumah > Soal Jawab > teks badan
P粉8181258052023-08-26 00:03:41
Saya membaca semula siaran sebelumnya11 Seterusnya dan menambah tag Skrip tidak berfungsi masukkan penerangan pautan di sini dan menyedari bahawa anda tidak boleh meletakkan komponen <Script>
dalam tag Ketua. Selain itu, ia tidak sepatutnya berada dalam _document.tsx tetapi dalam _app.tsx (melainkan anda menggunakan beforeInteractive, lihat pautan di atas).
Oleh kerana saya juga ingin memasukkan skrip Google Analitis, saya mencipta komponen yang dipanggil TrackingCode sebagai fail js yang berasingan.
import Script from "next/script"; function TrackingCode() { return ( <> {/* Global site tag (gtag.js) - Google Analytics */} <Script src="https://www.googletagmanager.com/gtag/js?id=G-GOOGLEID" /> <Script id="google-analytics" strategy="afterInteractive"> {` window.dataLayer = window.dataLayer || []; function gtag(){window.dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-GOOGLEID'); `} </Script> {/* for Zoho Marketing Automation */} <Script id="zoho-ma"> {`var w = window; var p = w.location.protocol; if (p.indexOf("http") < 0) { p = "http" + ":"; } var d = document; var f = d.getElementsByTagName("script")[0], s = d.createElement("script"); s.type = "text/javascript"; s.async = false; if (s.readyState) { s.onreadystatechange = function () { if (s.readyState == "loaded" || s.readyState == "complete") { s.onreadystatechange = null; try { loadwaprops( "mycode", "mycode", "mycode", "mycode", "0.0" ); } catch (e) {} } }; } else { s.onload = function () { try { loadwaprops( "mycode", "mycode", "mycode", "mycode", "0.0" ); } catch (e) {} }; } s.src = p + "//ma.zoho.com/hub/js/WebsiteAutomation.js"; f.parentNode.insertBefore(s, f);`} </Script> {/* end Zoho marketing automation */} </> ); } export default TrackingCode;
Fail _app.tsx saya adalah seperti berikut:
import "../assets/scss/material-kit.scss"; import "../node_modules/bootstrap/scss/bootstrap.scss"; import "../styles/globals.scss"; import { useEffect } from "react"; import type { ReactElement, ReactNode } from "react"; import type { NextPage } from "next"; import type { AppProps } from "next/app"; import TrackingCode from "../components/tracking-code"; import store from "../app/store"; export type NextPageWithLayout = NextPage & { getLayout?: (page: ReactElement) => ReactNode; }; type AppPropsWithLayout = AppProps & { Component: NextPageWithLayout; }; export default function MyApp({ Component, pageProps }: AppPropsWithLayout) { const getLayout = Component.getLayout ?? ((page) => page); useEffect(() => { typeof document !== undefined ? require("../node_modules/bootstrap/dist/js/bootstrap") : null; }, []); return getLayout( <> <TrackingCode /> <Component {...pageProps} /> </> ); }