繁星點(diǎn)點(diǎn)滴滴
2023-10-14 19:27:37
如何將道具從這里傳遞到要添加到 Head 組件下的導(dǎo)航欄組件?我已經(jīng)使用 getServerSideProps 在 index.js 頁(yè)面中完成了此操作,但它似乎在 _app.js 文件中不起作用。import "../_app.css";import React from "react";import PropTypes from "prop-types";import Head from "next/head";import { ThemeProvider } from "@material-ui/core/styles";import CssBaseline from "@material-ui/core/CssBaseline";import theme from "../theme";export default function MyApp({ Component, pageProps }) { React.useEffect(() => { // Remove the server-side injected CSS. const jssStyles = document.querySelector("#jss-server-side"); if (jssStyles) { jssStyles.parentElement.removeChild(jssStyles); } }, []); return ( <React.Fragment> <Head> <title>Furnibnz | Baldai Internetu ir Nemokamas Pristatymas</title> <meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" /> </Head> <ThemeProvider theme={theme}> {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */} <CssBaseline /> <Component {...pageProps} /> </ThemeProvider> </React.Fragment> );}MyApp.propTypes = { Component: PropTypes.elementType.isRequired, pageProps: PropTypes.object.isRequired,};有沒有更好的方法在每個(gè)頁(yè)面中使用導(dǎo)航欄組件,或者這是正確的方法嗎?
1 回答

炎炎設(shè)計(jì)
TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
當(dāng)這是我最大的問題時(shí)的美好時(shí)光哈哈
對(duì)于真正需要答案的人:
您當(dāng)前無法在 _app 組件中使用 getStaticProps 和 getServerSideProps ,因此,如果我沒記錯(cuò)的話,我會(huì)嘗試獲取初始數(shù)據(jù)(可能是類別列表)并將其傳遞給組件。要在 _app 級(jí)別獲取初始數(shù)據(jù),您應(yīng)該使用 getInitialProps(NextJs 開發(fā)人員建議避免使用它),但請(qǐng)注意,使用它會(huì)阻止 NextJs 自動(dòng)靜態(tài)優(yōu)化不依賴于任何類型獲取方法的頁(yè)面(它們將在服務(wù)器端呈現(xiàn) - 即使是一個(gè)簡(jiǎn)單的“關(guān)于我們”頁(yè)面,也可以作為靜態(tài) html 提供)
添加回答
舉報(bào)
0/150
提交
取消