gpt4 book ai didi

Next.js 使用 _app.js 和 _document.js

转载 作者:行者123 更新时间:2023-12-04 02:47:27 27 4
gpt4 key购买 nike

我目前使用_document.js将 css 注入(inject)到我的 Next.js 应用程序中,我想开始使用 _app.js帮助将数据注入(inject)页面。

想知道是否可以同时使用 _document 和 _app 还是应该只使用其中一个?

最佳答案

简短回答:是的,您可以同时使用两者。它们有不同的用途,可以在同一个应用程序中使用。
根据NextJS docs :

Next.js uses the App component to initialize pages.

To override, create the ./pages/_app.js file and override the App class



Pages in Next.js skip the definition of the surrounding document's markup. For example, you never include <html>, <body>, etc. To override that default behavior, you must create a file at ./pages/_document.js, where you can extend the Document class.


备注 : _document.js仅在服务器端呈现,而不在客户端呈现。所以像 onClick 这样的事件处理程序不会工作。

关于Next.js 使用 _app.js 和 _document.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51040669/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com