Javascript 如何在Next.js中将路径名从_文档传递到_应用程序
我是Next.js新手,在将数据从_文档传递到_应用程序时遇到了困难 我需要将路径名服务器端从Javascript 如何在Next.js中将路径名从_文档传递到_应用程序,javascript,reactjs,typescript,next.js,Javascript,Reactjs,Typescript,Next.js,我是Next.js新手,在将数据从_文档传递到_应用程序时遇到了困难 我需要将路径名服务器端从\u document.ts传递到\u app.ts,然后传递到app组件中,这样我就可以在Head元素服务器端插入自定义标记。每个页面都有特定的链接 例如 将显示在第页https://example.com/about-us 当前的实现如下所示: getInitialProps在_document.tx中 const sheets = new ServerStyleSheets();
\u document.ts
传递到\u app.ts
,然后传递到app
组件中,这样我就可以在Head
元素服务器端插入自定义标记。每个页面都有特定的链接
例如
将显示在第页<代码>https://example.com/about-us
当前的实现如下所示:
getInitialProps在_document.tx中
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
});
const { lang } = ctx.query;
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
pathname: ctx.asPath,
locale: getLocaleFromLanguage(lang as SupportedLanguages),
styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
};
当我console.log(pageProps)
我只得到eg{locale:'en'}
,没有从_document.ts传递的路径名
属性
你知道如何将道具从_文档传递到_应用程序吗我遇到了这个问题,我使用req对象将我的参数从_应用程序发送到_文档。根据NextJs解析顺序,我们可以使用req(IncomingMessage)对象。 服务器端的NextJs解析顺序:
(ctx.req as any).direction = organization.direction;
以及在_文档的getInitialProps中:
const direction = ctx.req.direction;
我也有同样的问题。你解决了吗?@elyas.m不,必须找到完全不同的方法。。
const direction = ctx.req.direction;