所以我已经看过书了,可以看到这是很平常的事情,但可惜的是我无法找到的所有解决方案都对我不起作用。
在npm运行开发人员模式下,项目很好,我的所有导入工作都可以。
import { Typography } from "@material-ui/core";
import Card from 'react-bootstrap/Card'
我如何导入页面的示例,但是第二次我npm运行构建并转到页面,看来这些导入失败了,我只是没有CSS。
这是我的next.config.js文件
const withCSS = require('@zeit/next-css')
module.exports = withCSS({
cssLoaderOptions: {
url: false
}
})
我以为我需要给它materialUI和react-bootstrap?我对此的尝试失败了。
对此的任何帮助将不胜感激,不确定为什么它无法构建。
这是我的package.json
{
"name": "name",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"prod": "next export"
},
"dependencies": {
"@material-ui/core": "^4.8.2",
"@material-ui/icons": "^4.5.1",
"@zeit/next-css": "^1.0.1",
"bootstrap": "^4.4.1",
"next": "9.1.6",
"next-compose-plugins": "^2.2.0",
"nextjs-sitemap-generator": "^0.4.2",
"react": "16.12.0",
"react-bootstrap": "^1.0.0-beta.16",
"react-dom": "16.12.0",
"styled-components": "^4.4.1"
}
}
这是material-ui
版以上的4.3
面临的常见问题。可以通过使用_document.js
预加载CSS来解决。
link中对此进行了广泛的描述。为了简洁起见,我在这里添加它。在_document.js
中添加以下代码,CSS应该看起来还不错。
import React from 'react'
import Document, { Html, Head, Main, NextScript } from 'next/document'
import { ServerStyleSheets } from '@material-ui/styles'
import { createMuiTheme, responsiveFontSizes } from '@material-ui/core/styles'
const theme = responsiveFontSizes(createMuiTheme())
class MyDocument extends Document {
render() {
return (
<Html>
<Head>
<meta charSet="utf-8" />
<meta
name="viewport"
content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no"
/>
<meta name="theme-color" content={theme.palette.primary.main} />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Roboto+Slab:400,700|Material+Icons"
/>
<style jsx global>
{`
html,
body {
height: 100%;
width: 100%;
}
*,
*:after,
*:before {
box-sizing: border-box;
}
body {
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
font-size: 1rem;
margin: 0;
}
`}
</style>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
MyDocument.getInitialProps = async ctx => {
// Render app and page and get the context of the page with collected side effects.
const sheets = new ServerStyleSheets()
const originalRenderPage = ctx.renderPage
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheets.collect(<App {...props} />)
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [
<React.Fragment key="styles">
{initialProps.styles}
{sheets.getStyleElement()}
</React.Fragment>
]
}
}
export default MyDocument