赞
踩
本文将指导您在 Angular 项目中集成 ESLint,并在 Visual Studio Code (VS Code) 中配置和使用 ESLint。
npm install -g @angular/cli
然后,进入您的项目目录并安装以下必要的 ESLint 相关依赖项:
npm install @angular-eslint/builder @angular-eslint/eslint-plugin @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint --save-dev
import typescriptEslintPlugin from '@typescript-eslint/eslint-plugin'; import typescriptEslintParser from '@typescript-eslint/parser'; /** @type {import('eslint').Linter.FlatConfig[]} */ const config = [ { files: ['**/*.ts'], // 仅针对 TypeScript 文件 languageOptions: { ecmaVersion: 2020, sourceType: 'module', parser: typescriptEslintParser, }, plugins: { '@typescript-eslint': typescriptEslintPlugin, }, rules: { 'semi': ['error', 'always'], // 确保使用分号 // 在此添加其他 ESLint 规则 }, }, ]; export default config;
{ "version": 1, "newProjectRoot": "projects", "projects": { "tamalelibs": { "root": "", "sourceRoot": "src", "projectType": "application", "prefix": "app", "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": { "outputPath": "dist/tamalelibs", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", "tsConfig": "src/tsconfig.app.json", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [], "scripts": [] }, "configurations": { "production": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" } ], "optimization": true, "outputHashing": "all", "sourceMap": false, "extractCss": true, "namedChunks": false, "aot": true, "extractLicenses": true, "vendorChunk": false, "buildOptimizer": true } } }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", "options": { "browserTarget": "tamalelibs:build" }, "configurations": { "production": { "browserTarget": "tamalelibs:build:production" } } }, "extract-i18n": { "builder": "@angular-devkit/build-angular:extract-i18n", "options": { "browserTarget": "tamalelibs:build" } }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "main": "src/test.ts", "polyfills": "src/polyfills.ts", "tsConfig": "src/tsconfig.spec.json", "karmaConfig": "src/ut.dev.conf.js", "styles": [], "scripts": [], "assets": [ "src/favicon.ico", "src/assets" ] } }, "lint": { "builder": "@angular-devkit/build-angular:eslint", "options": { "lintFilePatterns": [ "src/**/*.ts", "src/**/*.html" ] } } } }, "tamalelibs-e2e": { "root": "e2e/", "projectType": "application", "architect": { "e2e": { "builder": "@angular-devkit/build-angular:protractor", "options": { "protractorConfig": "e2e/protractor.conf.js", "devServerTarget": "tamalelibs:serve" }, "configurations": { "production": { "devServerTarget": "tamalelibs:serve:production" } } }, "lint": { "builder": "@angular-devkit/build-angular:eslint", "options": { "lintFilePatterns": [ "e2e/**/*.ts" ] } } } } }, "defaultProject": "tamalelibs" }
{
"window.zoomLevel": 0,
"editor.detectIndentation": false,
"editor.wordWrap": "on",
"editor.renderWhitespace": "boundary",
"editor.formatOnSave": false,
"typescript.preferences.quoteStyle": "single",
"typescript.preferences.importModuleSpecifier": "relative",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"files.trimTrailingWhitespace": true,
"editor.guides.indentation": true
}
确保已安装 ESLint VSCode 扩展程序。在 VSCode 中打开扩展视图,搜索 “ESLint” 并安装。
npx eslint "src/**/*.ts" --fix
您还可以通过在 package.json 中添加脚本来简化这个过程:
"scripts": {
// 其他脚本
"lint": "eslint \"src/**/*.ts\" --fix"
}
然后,您可以使用以下命令来运行 ESLint:
npm run lint
通过以上步骤,您已经在您的 Angular 项目中成功集成并配置了 ESLint,并在 VSCode 中设置了保存时自动修复代码。这将有助于确保您的代码风格一致,并减少代码中的潜在错误。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。