gpt4 book ai didi

node.js - NextJS 组件级 SASS 样式

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

我正在开发一个简单的 NextJS 应用程序,但是我在使用 SASS/SCSS 的组件级样式方面遇到了问题。当用户第一次登陆页面时一切看起来都很好,但是当用户导航到一个页面时 /about例如或来自 /about/ , 组件样式没有被渲染,用户需要刷新页面才能看到样式🤔

⚙️设置
./package.json

"dependencies": {
"@zeit/next-css": "1.0.1",
"@zeit/next-sass": "1.0.1",
"autoprefixer": "^9.7.0",
"next": "^9.3.1",
"node-sass": "4.13.1",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"webpack": "^4.42.0"
}
./next.config.js
const webpack = require('webpack');
const withSass = require('@zeit/next-sass');
const withCss = require('@zeit/next-css');

module.exports = withCss(
withSass({
webpack(config) {
config.module.rules.push({
test: /\.(eot|woff?2|ttf|otf|svg|png|jpe?g|gif)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
},
},
});
return config;
},
}),
);
./postcss.config.js
const autoprefixer = require('autoprefixer');

module.exports = {
plugins: [autoprefixer],
};

📄页面
./pages/index.js
import React from 'react';
import Layout from '../components/Layout';
import Home from '../components/Home';

class Index extends React.PureComponent {
render() {
<Layout>
<Home />
</Layout>
}
}

export default Index;
./pages/about.js
import React from 'react';
import Layout from '../components/Layout';
import Company from '../components/Company';

class About extends React.PureComponent {
render() {
<Layout>
<Company />
</Layout>
}
}

export default About;

📦 组件
./components/Layout.js
import React from 'react';
import AppBar from '../AppBar/AppBar';
import './Layout.scss';

class Layout extends React.PureComponent {
render() {
const { children } = this.props;

return (
<>
<AppBar />
<main className="Layout">
{children}
</main>
</>
);
}
}

export default Layout;
./components/Layout.scss
.Layout {
background: #f00;
}
./components/AppBar.js
import Link from 'next/link';
import './AppBar.scss';

export default () => (
<header className="AppBar">
<Link href="/">
<a>Home</a>
</Link>
<Link href="/about">
<a>About</a>
</Link>
</header>
)
./components/AppBar.scss
.AppBar {
background: #0f0;
}
./components/Home.js
import './Home.scss';

export default () => (
<div className="Home">Home</div>
)
./components/Home.scss
.Home {
background: #00f;
}
./components/Company.js
import './Company.scss';

export default () => (
<div className="Company">Company</div>
)
./components/Company.scss
.Company {
background: #ff0;
}

最佳答案

这就是我解决我的问题的方法。 🎉

随着 Next.js 9.3 的发布,不再需要 @zeit/next-sass & @zeit/next-css . 👏
我还执行以下操作:

  • 添加 styled-jsx-plugin-sass node-sassa作为开发部门
  • 创建 .babelrc文件并添加以下内容:
  • {
    "presets": [
    [
    "next/babel",
    {
    "styled-jsx": {
    "plugins": ["styled-jsx-plugin-sass"]
    }
    }
    ]
    ]
    }

    所以我有两种选择:
  • <style jsx> 上添加我的 sass/scss
  • 将我的组件 sass/scss 文件重命名为 <ComponentName>.module.scss

  • 第二个选项是一个痛苦的A,你需要 import styles from './ComponentName.module.scss并将元素类添加为 className={styles.ComponentName}className={styles['ComponentName-button']} . 🧐

    关于node.js - NextJS 组件级 SASS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60752431/

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