Nuxt項目支持eslint+pritter+typescript的實現(xiàn)
腳手架安裝好nuxt的基本項目
npx create-nuxt-app <項目名>
,如:npx create-nuxt-app nuxt-ts
,按照提示安裝你想要的東西,本次項目預(yù)裝: Universal模式下koa+PWA+linter+prettier+axios ,默認(rèn)的項目目錄如下:
eslint + prettier + vscode 保存自動格式化&修復(fù)
本人習(xí)慣縮進(jìn)為4個空格,但是eslint&nuxt生成的項目默認(rèn)為2個,因此需要更改配置
.editorconfig
文件下的indent_size: 2
更改為indent_size: 4
.vscode/settings.json
{ // 保存時eslint自動修復(fù)錯誤 "eslint.autoFixOnSave": true, // 保存自動格式化 "editor.formatOnSave": true, // 開啟 eslint 支持 "prettier.eslintIntegration": true, // prettier配置 --- 使用單引號【與.prettierrc下的配置對應(yīng)】 "prettier.singleQuote": true, //prettier配置 --- 結(jié)尾不加分號 【與.prettierrc下的配置對應(yīng)】 "prettier.semi": false, //prettier配置 --- 每行最多顯示的字符數(shù) 【與.prettierrc下的配置對應(yīng)】 "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" } }, //根據(jù)文件后綴名定義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, // 結(jié)尾不加分號 `.vscode/settings.json` 的`prettier.semi` "printWidth": 120 // 此項為我自加以上兩項為默認(rèn),表示每行最多顯示的字符數(shù),默認(rèn)為80,本人覺得太短了,因此修改了一下,必須與`.vscode/settings.json` 的`prettier.printWidth`對應(yīng)上 /* 更多配置請戳 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個空格縮進(jìn) /* 更多配置請戳 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.json
的 dev
腳本中 server/index.js
為 server/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
就會使用默認(rèn)值自動更新此配置文件
.eslintrc.js
修改.eslintrc.js
文件 parserOptions.parser: '@typescript-eslint/parser'
parserOptions: { parser: '@typescript-eslint/parser' },
修改.eslintrc.js
文件 plugins添加'@typescript-eslint'
plugins: ['prettier', '@typescript-eslint'],
nuxt.config.js
修改nuxt.config.js
文件后綴為 nuxt.config.ts
修改nuxt.config.ts
的build.extend
{ test: /\.ts$/, exclude: [/node_modules/, /vendor/, /\.nuxt/], loader: 'ts-loader', options: { appendTsSuffixTo: [/\.vue$/], transpileOnly: true } }
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 路徑 需要寫清楚后綴
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ElementUI中el-dropdown-item點擊事件無效問題
這篇文章主要介紹了ElementUI中el-dropdown-item點擊事件無效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04前端使用vue實現(xiàn)token無感刷新的三種方案解析
這篇文章主要為大家介紹了前端使用vue實現(xiàn)token無感刷新的三種方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vueJS簡單的點擊顯示與隱藏的效果【實現(xiàn)代碼】
下面小編就為大家?guī)硪黄獀ueJS簡單的點擊顯示與隱藏的效果【實現(xiàn)代碼】。小編覺得挺不錯的,現(xiàn)在分享給大家,一起跟隨小編過來看看吧2016-05-05Vue3 響應(yīng)式 API 及 reactive 和 ref&
響應(yīng)式是一種允許以聲明式的方式去適應(yīng)變化的編程范例,這篇文章主要介紹了關(guān)于Vue3響應(yīng)式API及reactive和ref的用法,需要的朋友可以參考下2023-06-06