温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Nuxt项目怎么支持eslint+pritter+typescript

发布时间:2021-07-07 11:40:36 阅读:349 作者:小新 栏目:web开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章将为大家详细讲解有关Nuxt项目怎么支持eslint+pritter+typescript,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

脚手架安装好nuxt的基本项目

npx create-nuxt-app <项目名>,如:npx create-nuxt-app nuxt-ts,按照提示安装你想要的东西,本次项目预装: Universal模式下koa+PWA+linter+prettier+axios ,默认的项目目录如下:

Nuxt项目怎么支持eslint+pritter+typescript

eslint + prettier + vscode 保存自动格式化&修复

本人习惯缩进为4个空格,但是eslint&nuxt生成的项目默认为2个,因此需要更改配置

  • .editorconfig文件下的indent_size: 2更改为indent_size: 4

  • .vscode/settings.json

{
 // 保存时eslint自动修复错误
 "eslint.autoFixOnSave"true,
 // 保存自动格式化
 "editor.formatOnSave"true,
 // 开启 eslint 支持
 "prettier.eslintIntegration"true,
 // prettier配置 --- 使用单引号【与.prettierrc下的配置对应】
 "prettier.singleQuote"true,
 //prettier配置 --- 结尾不加分号 【与.prettierrc下的配置对应】
 "prettier.semi"false,
 //prettier配置 --- 每行最多显示的字符数 【与.prettierrc下的配置对应】
 "prettier.printWidth"120,
 //.vue文件template格式化支持,并使用js-beautify-html插件
 "vetur.format.defaultFormatter.html""js-beautify-html",
 //js-beautify-html格式化配置,属性强制换行
 "vetur.format.defaultFormatterOptions": {
  "js-beautify-html": {
   // "wrap_attributes""force-aligned"
  }
 },
 //根据文件后缀名定义vue文件类型
 "files.associations": {
  "*.vue""vue"
 },
 //配置 ESLint 检查的文件类型
 "eslint.validate": [
  "javascript",
  "javascriptreact",
  {
   "language""vue",
   "autoFix"true
  },
  {
   "language""typescript",
   "autoFix"true
  }
 ],
 "files.autoSave""onFocusChange",
 "vetur.format.enable"false,
 "vetur.experimental.templateInterpolationService"true,
 "editor.detectIndentation"false
}

.prettierrc文件

{
 "singleQuote"true// 使用单引号 `.vscode/settings.json` 的`prettier.semi`
 "semi"false// 结尾不加分号 `.vscode/settings.json` 的`prettier.semi`
 "printWidth"120 // 此项为我自加以上两项为默认,表示每行最多显示的字符数,默认为80,本人觉得太短了,因此修改了一下,必须与`.vscode/settings.json` 的`prettier.printWidth`对应上
/* 更多配置请戳 https://prettier.io/docs/en/options.html */
}

.eslintrc.js文件配置

module.exports = {
 root: true,
 env: {
  browser: true,
  node: true
 },
 parserOptions: {
  parser: 'babel-eslint'
 },
 extends: [
  '@nuxtjs',
  'plugin:nuxt/recommended',
  'plugin:prettier/recommended',
  'prettier',
  'prettier/vue'
 ],
 plugins: ['prettier'],
 // add your custom rules here
 rules: {
  'nuxt/no-cjs-in-config''off',
  indent: ['error'4// 4个空格缩进
  /* 更多配置请戳 http://eslint.cn/docs/rules/ */
 }
}

nuxt.config.js文件下 build.extend(config, ctx) {}添加options.fix: true

 build: {
  /*
   ** You can extend webpack config here
   */
  extend(config, ctx) {
   // Run ESLint on save
   if (ctx.isDev && ctx.isClient) {
    config.module.rules.push({
     enforce'pre',
     test/\.(js|vue)$/,
     loader'eslint-loader',
     exclude/(node_modules)/,
     options: {
      fixtrue
     }
    })
   }
  }
 }

开始改造工程支持typescript

安装所需插件

  • npm i -D @nuxt/typescript ts-node @typescript-eslint/eslint-plugin

  • npm install -S vue-class-component vue-property-decorator

修改&添加配置

package.json

添加或编辑package.json的lint脚本:
"lint": "eslint --ext .ts,.js,.vue --ignore-path .gitignore ."

修改package.jsondev 脚本中 server/index.jsserver/index.ts

"dev": "cross-env NODE_ENV=development nodemon server/index.ts --watch server",

tsconfig.json

项目目录下新建tsconfig.json文件后,在package.json文件下添加:
"start-dev": "nuxt" 脚本命令,运行npm run dev就会使用默认值自动更新此配置文件

.eslintrc.js

修改.eslintrc.js文件 parserOptions.parser: '@typescript-eslint/parser'

parserOptions: {
 parser: '@typescript-eslint/parser'
},

修改.eslintrc.js文件 plugins添加'@typescript-eslint'

plugins: ['prettier''@typescript-eslint'],

Nuxt项目怎么支持eslint+pritter+typescript

nuxt.config.js

修改nuxt.config.js文件后缀为 nuxt.config.ts

修改nuxt.config.tsbuild.extend

{
 test/\.ts$/,
 exclude: [/node_modules//vendor//\.nuxt/],
 loader'ts-loader',
 options: {
  appendTsSuffixTo: [/\.vue$/],
  transpileOnlytrue
 }
}

Nuxt项目怎么支持eslint+pritter+typescript

server/index.js

修改server/index.js文件后缀为 server/index.ts

修改server/index.ts中的

const config = require('../nuxt.config.js')

// 为

const config = require('../nuxt.config.ts')

修改vue文件为typescript语法

<script>
import Logo from '~/components/Logo.vue'

export default {
 components: {
  Logo
 }
}
</script>

typescript 语法如下:

<script lang="ts">
import { ComponentVue } from 'vue-property-decorator'
import Logo from '~/components/Logo.vue'

@Component({
 components: {
  Logo
 },
 middleware'check-auth'
})
export default class IndexPage extends Vue {}
</script>

坑点

vetur 报错 Cannot find module 'xxxx'

解决方案:import 路径 需要写清楚后缀

Nuxt项目怎么支持eslint+pritter+typescript

关于“Nuxt项目怎么支持eslint+pritter+typescript”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×