要在 Nuxt 中安装 SASS,您必须运行yarn add -D sass [email protected] /cdn-cgi/l/email-protection
(or npm i -D [email protected] /cdn-cgi/l/email-protection --save-exact && npm i -D sass
).
的版本sass-loader
需要准确并最迟设置10.x.x
因为下一个(11.0.0
)正在使用 Webpack5,因此这是一个重大更改,因为 Nuxt2 仅在 Webpack4 上运行,如下所示:https://github.com/webpack-contrib/sass-loader/releases https://github.com/webpack-contrib/sass-loader/releases
IF那么,你仍然无法使用<style lang="sass">
在你的.vue
组件,然后继续。
将其添加到您的nuxt.config.js
file
export default {
build: {
loaders: {
sass: {
implementation: require('sass'),
},
scss: {
implementation: require('sass'),
},
},
}
}
这里有一个工作回购 https://github.com/kissu/so-nuxt-working-sass-scss以及最新推荐的sass
(达特萨斯 https://github.com/sass/dart-sass)设置可以正常使用这种代码
<template>
<div>
<span class="test">
Hello there
</span>
</div>
</template>
<style lang="sass" scoped>
div
.test
color: red
</style>
PS:如果正确安装了 SASS,那么 SCSS 也能正常工作,因为它基本上是相同的东西。
如果您对某些已弃用的内容有一些警告,例如/ for divison
或此处列出的任何内容:https://sass-lang.com/documentation/writing-changes https://sass-lang.com/documentation/breaking-changes
您可以参考这个答案进行修复:https://stackoverflow.com/a/68648204/8816585 https://stackoverflow.com/a/68648204/8816585