What does _app.js and _document.js file do? What's "_" (Underscore) for in next.js?

huangapple go评论68阅读模式
英文:

What does _app.js and _document.js file do? What's "_" (Underscore) for in next.js?

问题

以下是翻译的内容:

当我使用npx create-next-app创建一个Next.js应用程序时,会有两个以下划线开头的JS文件,分别命名为app和document,这是用来做什么的?

这两个文件的结构如下:

▼ 页面
  _app.js
  _document.js
英文:

When I created a next.js app using npx create-next-app there is 2 js files named app and document leading with an Underscore what is that for?

The files are like this:

▼ pages
  _app.js
  _document.js

答案1

得分: 2

这是一种约定。 _ 表示文件是部分的,不应该暴露给用户作为页面。

以下是来自Next.js的一位贡献者的引用。

实际上,我们使用下划线(_)前缀表示“私有”(内部的,但实际上并不是机密的)页面,这基于通常使用下划线来表示这种约定。来源

英文:

It is a matter of convention. _ indicates that the file is partial and not supposed to be exposed as a page to the user.

Here is a quote from one of the contributors of Next.js.
> We actually use the underscore (_) prefix for 'private' (internal, but not actually secret) pages, based on common convention of underscores often being used to denote that.

Source

答案2

得分: 0

通过使用下划线前缀命名这些文件,Next.js 表示它们是特殊文件,不应该由用户直接访问。相反,它们应该被用作模板或样板代码,可以根据您的应用程序的特定需求进行修改。

英文:

By naming these files with an underscore prefix, Next.js indicates that they are special files that should not be directly accessed by the user. Instead, they are meant to be used as templates or boilerplate codes that can be modified to fit the specific needs of your application.

huangapple
  • 本文由 发表于 2023年2月24日 03:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定