Nuxt项目支持eslint+pritter+typescript的实现


Posted in Javascript onMay 20, 2019

脚手架安装好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: {
      fix: true
     }
    })
   }
  }
 }

开始改造工程支持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$/],
  transpileOnly: true
 }
}

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 { Component, Vue } 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的实现

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
Prototype使用指南之form.js
Jan 10 Javascript
JavaScript中通过闭包解决只能取得包含函数中任何变量最后一个值的问题
Aug 12 Javascript
js获得网页背景色和字体色的方法
Mar 21 Javascript
基于jQuery仿淘宝产品图片放大镜特效
Oct 19 Javascript
JavaScript仿商城实现图片广告轮播实例代码
Feb 06 Javascript
easyui关于validatebox实现多重规则验证的方法(必看)
Apr 12 Javascript
使用OPENLAYERS3实现点选的方法
Sep 24 Javascript
React-Native做一个文本输入框组件的实现代码
Aug 10 Javascript
JS闭包的几种常见形式实例详解
Sep 16 Javascript
ES6中的Promise代码详解
Oct 09 Javascript
vue监听input标签的value值方法
Aug 27 Javascript
微信公众平台 客服接口发消息的实现代码(Java接口开发)
Apr 17 Javascript
vue3.0 搭建项目总结(详细步骤)
May 20 #Javascript
vue-cli webpack配置文件分析
May 20 #Javascript
微信小程序开发之左右分栏效果的实例代码
May 20 #Javascript
微信小程序rich-text富文本用法实例分析
May 20 #Javascript
bootstrap中的导航条实例代码详解
May 20 #Javascript
详解小程序云开发数据库
May 20 #Javascript
VUE脚手架具体使用方法
May 20 #Javascript
You might like
php各种编码集详解和以及在什么情况下进行使用
2011/09/11 PHP
ThinkPHP路由机制简介
2016/03/23 PHP
php检查函数必传参数是否存在的实例详解
2017/08/28 PHP
php实现的AES加密类定义与用法示例
2018/01/29 PHP
php web环境和命令行环境下查找php.ini的位置
2019/07/17 PHP
禁止js文件缓存的代码
2010/04/09 Javascript
Javascript的getYear、getFullYear、getUTCFullYear异同分享
2011/11/30 Javascript
JS正则验证邮箱的格式详细介绍
2013/11/19 Javascript
jQuery拖动div、移动div、弹出层实现原理及示例
2014/04/08 Javascript
Javascript中的五种数据类型详解
2014/12/26 Javascript
jquery通过closest选择器修改上级元素的方法
2015/03/17 Javascript
js限制input标签中只能输入中文
2015/06/26 Javascript
JS模拟酷狗音乐播放器收缩折叠关闭效果代码
2015/10/29 Javascript
JavaScript获取各大浏览器信息图示
2015/11/20 Javascript
用WebStorm进行Angularjs 2开发(环境篇:Windows 10,Angular-cli方式)
2018/12/05 Javascript
jquery的$().each和$.each的区别
2019/01/18 jQuery
VSCode写vue项目一键生成.vue模版,修改定义其他模板的方法
2020/04/17 Javascript
粗略分析Python中的内存泄漏
2015/04/23 Python
python实现FTP服务器服务的方法
2017/04/11 Python
pandas string转dataframe的方法
2018/04/11 Python
Python寻找两个有序数组的中位数实例详解
2018/12/05 Python
python使用for循环计算0-100的整数的和方法
2019/02/01 Python
Django时区详解
2019/07/24 Python
Windows 下更改 jupyterlab 默认启动位置的教程详解
2020/05/18 Python
宝塔面板成功部署Django项目流程(图文)
2020/06/22 Python
Python如何实现自带HTTP文件传输服务
2020/07/08 Python
使用CSS3编写灰阶滤镜来制作黑白照片效果的方法
2016/05/09 HTML / CSS
Merchant 1948澳大利亚:新西兰领先的鞋类和靴子供应商
2018/03/24 全球购物
Gtech官方网站:地毯清洁器、吸尘器及园艺设备
2018/05/23 全球购物
德国最新街头服饰网上商店:BODYCHECK
2019/09/15 全球购物
如何写毕业求职自荐信
2013/11/06 职场文书
研究生求职自荐书
2014/06/23 职场文书
研讨会通知
2015/04/27 职场文书
毕业论文答辩开场白和结束语
2015/05/27 职场文书
2016年五一劳动节专题校园广播稿
2015/12/17 职场文书
三八红旗手先进事迹材料(2016推荐版)
2016/02/25 职场文书