简体   繁体   English

如何从Next.js 13的新app文件夹中的公用文件夹中导入CSS个文件?

[英]How to import CSS files from public folder in the new app folder of Next.js 13?

In a 'old fashion' way (utilising) /pages I would edit _app.js files under /pages directory adding something like:以“老式”方式(利用) /pages ,我会编辑/pages目录下的_app.js文件,添加如下内容:

import Head from 'next/head'
export default function MyApp({ Component, pageProps }) {
  return <> 
<Head>
<link rel="stylesheet" href="/css/styles.css" />
      </Head>
<Component {...pageProps} />
</>
}
  

However in a new version there is no /pages/_app.js file.但是在新版本中没有/pages/_app.js文件。 There are layout.js and head.js instead.layout.jshead.js代替。 When I try to add in head.js (next to /favicon.ico) import I am getting error:当我尝试添加head.js (在 /favicon.ico 旁边)导入时出现错误:

react_devtools_backend.js:4012 Warning: Cannot render a outside the main document without knowing its precedence. react_devtools_backend.js:4012 警告:无法在不知道其优先级的情况下在主文档之外呈现。 Consider adding precedence="default" or moving it into the root tag.考虑添加 precedence="default" 或将其移至根标记中。

And when I try to import in layout.js file as below:当我尝试导入layout.js文件时,如下所示:

import "/css/styles.css";

I am getting compile error:我收到编译错误:

Module not found: Can't resolve '/css/styles.css'找不到模块:无法解析“/css/styles.css”

I was checking the official docs but they suggest only how to import from global styles (../../..) and not from public folder.我正在查看官方文档,但他们只建议如何从全局 styles (../../..) 而不是从公共文件夹导入。

As stated in the official doc here you need to create the pages/_app.js file if not already present.如此处的官方文档所述您需要创建pages/_app.js文件(如果不存在)。

Then you can import as such:然后你可以这样导入:

import Head from 'next/head'
export default function MyApp({ Component, pageProps }) {
  return <> 
<Head>
<link rel="stylesheet" href="/css/styles.css" />
      </Head>
<Component {...pageProps} />
</>
}

You can use../.. to import from the public folder.您可以使用 ../.. 从public导入。

With the directory structure与目录结构

/[project root]
  /public
    /css
      - styles.css
  /app
    - layout.js

You can import your styles using import '../public/css/styles.css'您可以使用import '../public/css/styles.css'导入您的 styles

If you need a CSS that gets imported from public folder, you can do it in head.js as you said, but you need to add precedence="default" to your link , as they say in the documentation :如果您需要从public导入的 CSS,您可以按照您所说的那样在head.js中执行此操作,但您需要在link中添加precedence="default" ,正如他们在文档中所说:

If you want to manually place, it needs a precedence field which is a new React-specific attribute <link rel="stylesheet" precedence="default" /> .如果你想手动放置,它需要一个优先字段,它是一个新的 React 特定属性<link rel="stylesheet" precedence="default" />

// in head.js alongside `layout.js` and `page.js`

export default function Head() {
  return (
    <>
      <title>Create Next App</title>
      <meta content="width=device-width, initial-scale=1" name="viewport" />
      <meta name="description" content="Generated by create next app" />
      <link rel="icon" href="/favicon.ico" />
      <link rel="stylesheet" href="/css/styles.css" precedence="default" />
    </>
  );
}

You may get an Eslint warning saying "Do not include stylesheets manually" that you can deactivate by adding this comment:您可能会收到 Eslint 警告“不要手动包含样式表” ,您可以通过添加以下注释来停用该警告:

{/* eslint-disable-next-line @next/next/no-css-tags */}
<link rel="stylesheet" href="/css/styles.css" precedence="default" />

Also, as you may already know, including a css file like this doesn't allow to have live reloading when you change its content, for example.此外,您可能已经知道,例如,包含这样的 css 文件不允许在您更改其内容时进行实时重新加载。 So to be used for external CSS that won't often.所以要用于不会经常使用的外部 CSS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Next.js不能导入文件夹下的文件 - Why can't Next.js import files under a folder 如何将页面/应用程序文件夹重定向到 next.js 中的子域 - How to redirect pages/app folder to subdomain in next.js 为什么要将图像放入 Next.js 公共文件夹? 有优势吗? - Why put images into Next.js public folder? Is there an advantage? 无法从 CSS 创建 React App 中的公用文件夹导入 - Cannot import from public folder in CSS Create React App 使用 Next.js 中的 getStaticProps 对来自公共文件夹的 static 图像进行编码 - Encoding static imagery from public folder using getStaticProps in Next.js 将文件移动到 Next.js 中的 src/pages 文件夹时,自定义 _app 和 _document 将被忽略 - Custom _app and _document get ignored when moving files to src/pages folder in Next.js 使用 Next.js 中同一文件夹中的图像 in.md 文件 - using image in .md file from the same folder in Next.js 下一步.js。 如何从项目外的文件夹中导入模块? - Next.js. How to import a module from the folder outside of the project? 如何在 Next.js 的 pages 文件夹中获取动态路由路径? - How to get dynamic route path in pages folder in Next.js? 如何使用 Next.js 从组件文件夹中获取 API 详细信息? - How to fetch the API details from a component folder using Next.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM