Nuxt項(xiàng)目支持eslint+pritter+typescript的實(shí)現(xiàn)
腳手架安裝好nuxt的基本項(xiàng)目
npx create-nuxt-app <項(xiàng)目名>,如:npx create-nuxt-app nuxt-ts,按照提示安裝你想要的東西,本次項(xiàng)目預(yù)裝: Universal模式下koa+PWA+linter+prettier+axios ,默認(rèn)的項(xiàng)目目錄如下:

eslint + prettier + vscode 保存自動(dòng)格式化&修復(fù)
本人習(xí)慣縮進(jìn)為4個(gè)空格,但是eslint&nuxt生成的項(xiàng)目默認(rèn)為2個(gè),因此需要更改配置
.editorconfig文件下的indent_size: 2更改為indent_size: 4.vscode/settings.json
{
// 保存時(shí)eslint自動(dòng)修復(fù)錯(cuò)誤
"eslint.autoFixOnSave": true,
// 保存自動(dòng)格式化
"editor.formatOnSave": true,
// 開啟 eslint 支持
"prettier.eslintIntegration": true,
// prettier配置 --- 使用單引號(hào)【與.prettierrc下的配置對(duì)應(yīng)】
"prettier.singleQuote": true,
//prettier配置 --- 結(jié)尾不加分號(hào) 【與.prettierrc下的配置對(duì)應(yīng)】
"prettier.semi": false,
//prettier配置 --- 每行最多顯示的字符數(shù) 【與.prettierrc下的配置對(duì)應(yīng)】
"prettier.printWidth": 120,
//.vue文件template格式化支持,并使用js-beautify-html插件
"vetur.format.defaultFormatter.html": "js-beautify-html",
//js-beautify-html格式化配置,屬性強(qiáng)制換行
"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, // 使用單引號(hào) `.vscode/settings.json` 的`prettier.semi`
"semi": false, // 結(jié)尾不加分號(hào) `.vscode/settings.json` 的`prettier.semi`
"printWidth": 120 // 此項(xiàng)為我自加以上兩項(xiàng)為默認(rèn),表示每行最多顯示的字符數(shù),默認(rèn)為80,本人覺得太短了,因此修改了一下,必須與`.vscode/settings.json` 的`prettier.printWidth`對(duì)應(yīng)上
/* 更多配置請(qǐ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個(gè)空格縮進(jìn)
/* 更多配置請(qǐng)戳 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
項(xiàng)目目錄下新建tsconfig.json文件后,在package.json文件下添加:
"start-dev": "nuxt" 腳本命令,運(yùn)行npm run dev就會(huì)使用默認(rèn)值自動(dòng)更新此配置文件
.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語(yǔ)法
<script>
import Logo from '~/components/Logo.vue'
export default {
components: {
Logo
}
}
</script>
typescript 語(yǔ)法如下:
<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>
坑點(diǎn)
vetur 報(bào)錯(cuò) Cannot find module 'xxxx'
解決方案:import 路徑 需要寫清楚后綴

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+pinia用戶信息持久緩存token的問(wèn)題解決
本文主要介紹了vue3+pinia用戶信息持久緩存token的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Vue nextTick延遲回調(diào)獲取更新后DOM機(jī)制詳解
這篇文章主要為大家介紹了Vue nextTick延遲回調(diào)獲取更新后DOM機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Vue實(shí)現(xiàn)類似Spring官網(wǎng)圖片滑動(dòng)效果方法
這篇文章主要介紹了Vue實(shí)現(xiàn)類似Spring官網(wǎng)圖片滑動(dòng)效果方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
Vue生產(chǎn)和開發(fā)環(huán)境如何切換及過(guò)濾器的使用
本文主要介紹了Vue生產(chǎn)、開發(fā)環(huán)境如何切換及過(guò)濾器的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
使用typescript構(gòu)建Vue應(yīng)用的實(shí)現(xiàn)
這篇文章主要介紹了使用typescript構(gòu)建Vue應(yīng)用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
vue中解決chrome瀏覽器自動(dòng)播放音頻和MP3語(yǔ)音打包到線上的實(shí)現(xiàn)方法
這篇文章主要介紹了vue中解決chrome瀏覽器自動(dòng)播放音頻和MP3語(yǔ)音打包到線上的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

