面试题答案
一键面试- 安装依赖:
- 确保项目中安装了
next - sass
和sass
。可以通过npm或yarn进行安装。 - 使用npm:
npm install next - sass sass
- 使用yarn:
yarn add next - sass sass
- 确保项目中安装了
- 创建全局样式文件:
- 在项目根目录下的
styles
文件夹(如果没有则创建)中,创建一个全局样式文件,例如globals.scss
。 - 在
globals.scss
中编写全局样式,比如:
body { font - family: Arial, sans - serif; margin: 0; padding: 0; }
- 在项目根目录下的
- 修改
_app.js
文件:- 在
pages/_app.js
文件中导入全局样式文件。 - 如果
_app.js
内容如下:
import type { AppProps } from 'next/app'; function MyApp({ Component, pageProps }: AppProps) { return <Component {...pageProps} />; } export default MyApp;
- 修改为:
import type { AppProps } from 'next/app'; import '../styles/globals.scss'; function MyApp({ Component, pageProps }: AppProps) { return <Component {...pageProps} />; } export default MyApp;
- 在
- 配置
next.config.js
:- 如果项目根目录下没有
next.config.js
文件,则创建该文件。 - 在
next.config.js
中添加如下配置:
const withSass = require('next - sass'); module.exports = withSass({ // 其他next.js配置可以继续添加在这里 });
- 如果项目根目录下没有