在使用 vscode 时 eslint 检测 .vue 文件中的less 部分内容

问题:

在使用 vscode 以及 eslint 来检测 基于 webpack 的 vue-cli 的项目中,eslint 无法检测到  .vue 文件中的less 部分内容。
解答:

1、通过 下载 一个 vscode  插件 vetur

2、然后在  user setting 里面添加一 段 代码

3、
    "files.associations": {
        "*.vue": "vue"
    }

转载于:https://www.cnblogs.com/erbingbing/p/6253089.html

### 配置 Vue3 + TypeScript + Less 项目以实现 Stylelint 使用 order-smacss 插件 在 Vue3 + TypeScript + Less 的项目中,可以通过以下配置实现 Stylelint 使用 `order-smacss` 插件对 `.css`、`.less` 和 `.vue` 文件进行样式自动排序和格式化。以下是具体的配置方法: #### 1. 安装依赖 首先需要安装与 Stylelint 相关的插件和工具: ```bash npm install --save-dev stylelint stylelint-config-standard stylelint-order stylelint-config-sass-guidelines stylelint-config-recess-order stylelint-config-smacss stylelint-declaration-strict-value postcss-less ``` 这些依赖包括: - `stylelint`: 核心工具。 - `stylelint-config-standard`: 基础规则集。 - `stylelint-order`: 提供样式属性顺序校验。 - `stylelint-config-sass-guidelines`: 支持 Sass/SCSS 规则。 - `stylelint-config-recess-order`: 按照特定顺序排列样式属性。 - `stylelint-config-smacss`: SMACSS 设计原则支持。 - `postcss-less`: 支持 `.less` 文件解析。 #### 2. 配置 Stylelint 在项目根目录下创建或更新 `.stylelintrc.js` 文件,添加以下内容: ```javascript module.exports = { extends: [ 'stylelint-config-standard', 'stylelint-config-sass-guidelines', 'stylelint-config-recess-order', 'stylelint-config-smacss' ], plugins: ['stylelint-order'], rules: { // 属性顺序规则 'order/order': ['dollar-variables', 'declarations', 'rules'], 'order/properties-order': [ 'display', 'position', 'top', 'right', 'bottom', 'left', 'z-index', 'box-sizing', 'width', 'height', 'max-width', 'max-height', 'margin', 'padding', 'border', 'background', 'color', 'font-family', 'font-size', 'line-height', 'text-align', 'vertical-align', 'overflow', 'white-space', 'transition', 'animation' ], // 禁止无效的声明 'declaration-no-important': true, // 强制缩进为 2 个空格 'indentation': 2, // 禁止未知的 at 规则 'at-rule-no-unknown': true, // 强制分号结尾 'block-closing-brace-empty-line-before': 'never', 'declaration-colon-space-after': 'always-single-line', 'declaration-block-trailing-semicolon': 'always', 'rule-empty-line-before': 'always-multi-line' } }; ``` 上述配置使用了多个扩展规则集,并通过 `plugins` 引入 `stylelint-order` 插件[^1]。 #### 3. 配置 VSCode 自动格式化 为了确保保存文件自动格式化代码,需要在项目根目录下的 `.vscode/settings.json` 文件中添加以下配置: ```json { "typescript.tsdk": "node_modules/typescript/lib", "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.fixAll.eslint": "explicit", "source.fixAll.stylelint": "explicit" }, "stylelint.validate": ["css", "scss", "less", "vue"], "[vue]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[less]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[css]": { "editor.defaultFormatter": "esbenp.prettier-vscode" } } ``` 此配置确保 `.css`、`.less` 和 `.vue` 文件在保存会触发 Stylelint 的自动修复功能[^3]。 #### 4. 配置 Vite 支持 Less 如果项目使用 Vite 构建工具,需要在 `vite.config.js` 中添加对 Less 的支持: ```javascript import vue from '@vitejs/plugin-vue'; import styleImport from 'vite-plugin-style-import'; export default { plugins: [ vue(), styleImport({ libs: [ { libraryName: 'vant', esModule: true, resolveStyle: (name) => `vant/es/${name}/style`, }, ], }), ], css: { preprocessorOptions: { less: { javascriptEnabled: true, }, }, }, }; ``` 此配置启用了 Less 的预处理器选项,并允许 JavaScriptLess 文件中运行[^1]。 #### 5. 测试配置 完成上述配置后,可以通过以下命令测试 Stylelint 是否正常工作: ```bash npx stylelint "**/*.{css,less,vue}" --fix ``` 此命令会对指定文件类型执行检查并尝试自动修复问题。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值