我已经安装了这些依赖项 包.json:
{
"devDependencies": {
"@intlify/nuxt3": "^0.1.6",
"@nuxtjs/eslint-config": "^7.0.0",
"@nuxtjs/eslint-module": "^3.0.2",
"eslint": "^8.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-nuxt": "^3.0.0",
"eslint-plugin-vue": "^7.20.0",
"nuxt3": "latest",
"prettier": "2.4.1",
"sass": "^1.43.3",
"vite-plugin-eslint": "^1.3.0"
}
}
在.eslintrc.js
extends: [
'eslint:recommended',
'plugin:nuxt/recommended',
'prettier'
],
在 nuxt.config.ts
import eslintPlugin from 'vite-plugin-eslint';
export default defineNuxtConfig({
...
vite: {
plugins: [eslintPlugin()]
},
buildModules: ['@intlify/nuxt3', '@nuxtjs/eslint-module',],
})
而且这些选项都不适用于 nuxt 3.
一个简单的 ESLint + Prettier + TypeScript + Nuxt 3(或 Bridge)设置如下所示:
yarn add --dev eslint prettier eslint-config-prettier eslint-plugin-prettier @nuxtjs/eslint-config-typescript
.eslintrc.js
module.exports = {
root: true,
extends: ['@nuxtjs/eslint-config-typescript', 'plugin:prettier/recommended'],
}
package.json
{
"scripts": {
"lint": "eslint --ext .ts,.js,.vue ."
}
}
我的配置非常简单,基于官方的 nuxt git repos。
// nuxt.config.ts
export default defineNuxtConfig({
modules: [`@nuxtjs/eslint-module`]
});
和
// .eslintrc
{
"extends": ["@nuxt/eslint-config"]
}
和
// package.json
{
"name": "nuxt-app",
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare"
},
"devDependencies": {
"@nuxt/eslint-config": "^0.1.1",
"@nuxtjs/eslint-module": "^4.0.2",
"@types/node": "^18",
"eslint": "^8.39.0",
"nuxt": "^3.4.3",
"prettier": "^2.8.8"
}
}
这是我在这里找到的配置:https://github.com/nuxt/framework/discussions/2815#discussioncomment-2050408
// .eslintrc.json
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:vue/vue3-recommended",
"plugin:@typescript-eslint/recommended",
"@nuxtjs/eslint-config-typescript"
],
"parserOptions": {
"ecmaVersion": "latest",
"parser": "@typescript-eslint/parser",
"sourceType": "module"
},
"plugins": [
"vue",
"@typescript-eslint"
],
"rules": {}
}
如果你真的想使用 prettier(imo eslint 已经完成了这项工作,同时使用两者配置起来会很烦人)你可以添加 eslint-plugin-prettier 库,然后将“plugin:prettier/recommended”添加到 eslint extends。
不知道您使用的是什么 IDE,但如果它是 vscode,我建议您使用 linting on save 而不是依赖格式化程序(Volar、prettier、eslint-prettier)。 主要是因为它强制所有开发者使用相同的格式和规则