简体   繁体   English

如何从 Next.js 中的 css 模块导入样式表?

[英]How to import stylesheet from css modules in Next.js?

I am trying to integrate React Grid Layout in my Next.js application.我正在尝试将React Grid Layout集成到我的 Next.js 应用程序中。 As part of the installation process, they have said, include the following stylesheet in my application:他们说,作为安装过程的一部分,在我的应用程序中包含以下样式表:

/node_modules/react-grid-layout/css/styles.css
/node_modules/react-resizable/css/styles.css

How do I go about doing this?我该怎么做?

I haven't used this library myself, but normally to import css you would just import the file at the top of the module where you want to use it, and then you don't have to do anything else with it.我自己没有使用过这个库,但通常要导入 css,您只需在要使用它的模块顶部导入文件,然后您就不必对它做任何其他事情。

Assuming your App file is directly under src , if I was you I'd try importing those files at the top of App with:假设您的App文件直接位于src下,如果我是您,我会尝试在App顶部导入这些文件:

import '../node_modules/react-grid-layout/css/styles.css'
import '../node_modules/react-resizable/css/styles.css'

Well, actually they are not CSS modules and they are common CSS files, so you can simply import them into your root file (App.js or index.js) with ECMAscript module like this:好吧,实际上它们不是 CSS 模块,它们是常见的 CSS 文件,因此您可以使用 ECMAscript 模块将它们简单地导入到您的根文件(App.js 或 index.js)中,如下所示:

import '../node_modules/react-grid-layout/css/styles.css';
import '../node_modules/react-resizable/css/styles.css';

While @SMAKSS's answer is correct, I want to point out that there will be a new update from Next.js to support importing CSS from 3rd party libraries.虽然@SMAKSS 的回答是正确的,但我想指出 Next.js 会有一个新的更新来支持从 3rd 方库导入 CSS。 It is still on the canary branch at the time of this post is submitted.在提交这篇文章时,它仍然在金丝雀分支上。

Example例子

// components/MySlider.tsx
import { Slider } from "@reach/slider";
import "@reach/slider/styles.css";

function Example() {
  return <Slider min={0} max={200} step={10} />;
}

https://github.com/vercel/next.js/pull/16993 https://github.com/vercel/next.js/pull/16993

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM