解决 Vue3 + stylelint14 + SCSS + VSCode 没效果和报错 Unknown word (CssSyntaxError)

背景

现在网上配置教程都不是 stylelint14 版本的,导致网友参考之后默认安装了最新的版本,然后出现了各种奇怪的问题。

原因

安装的 stylelintVSCode Stylelint 插件版本太新,stylelint v14 及以上版本不兼容 vue3

解决办法1(推荐)

  1. 把之前的 stylelint 相关插件都卸载掉。
  2. 执行 npm install --save-dev postcss-scss postcss-html stylelint-config-recommended-vue stylelint-config-standard-scss stylelint 安装包。
  3. 配置刚才安装的包,可参考我下面的配置文件 stylelint.config.js,我还安装配置了 stylelint-config-recess-order 用于自动排序 CSS 如你不需要可忽略这个:
module.exports = {
  extends: [
    "stylelint-config-standard-scss",
    "stylelint-config-recommended-vue/scss",
    "stylelint-config-recess-order"
  ]
};
  1. 如果你用的是 VSCode 编辑器则需要在配置文件 setting.json 加上
// stylelint 需要检查的文件
"stylelint.validate": [
  "css",
  "less",
  "postcss",
  "scss",
  "vue",
  "sass"
],

参考来自官方说明 https://github.com/ota-meshi/stylelint-config-recommended-vuehttps://github.com/stylelint/stylelint/blob/14.0.0/docs/migration-guide/to-14.md#syntax-option-and-automatic-inferral-of-syntax

解决办法2

VSCode Stylelint 插件降级,并且将 stylelint 版本降级到 v13 及以下。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Vue3 + TypeScript + Vite 项目中使用 SCSS 预处理器,你需要遵循以下步骤: 1. 在项目中安装 `sass` 和 `sass-loader`: ``` npm install sass sass-loader --save-dev ``` 2. 确保你的 `tsconfig.json` 中开启了 `"experimentalDecorators": true` 和 `"emitDecoratorMetadata": true` 选项。 3. 在 `vite.config.ts` 中配置 `sass-loader`: ```typescript import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; import path from 'path'; export default defineConfig({ plugins: [vue()], resolve: { alias: { '@': path.resolve(__dirname, 'src') } }, css: { preprocessorOptions: { scss: { additionalData: `@import "@/styles/main.scss";` } } } }); ``` 在这个例子中,我们将 `main.scss` 文件导入到所有的 SCSS 文件中。 4. 创建一个 `main.scss` 文件,并在其中包含您的 SCSS 样式。 ```scss // Variables $primary-color: #007bff; $secondary-color: #6c757d; // Mixins @mixin button-variant($background-color) { background-color: $background-color; color: white; border: none; border-radius: 4px; padding: 10px 20px; cursor: pointer; transition: background-color 0.3s ease; &:hover { background-color: lighten($background-color, 10%); } &:active { background-color: darken($background-color, 10%); } } // Styles .primary-button { @include button-variant($primary-color); } .secondary-button { @include button-variant($secondary-color); } ``` 5. 在组件中使用您的 SCSS 样式。 ```vue <template> <div> <button class="primary-button">Primary Button</button> <button class="secondary-button">Secondary Button</button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; export default defineComponent({ name: 'MyComponent' }); </script> <style lang="scss"> /* Additional styles for this component */ </style> ``` 这样,您就可以在 Vue3 + TypeScript + Vite 项目中使用 SCSS 预处理器,并为每个组件编写自己的样式。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值