欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nuxt項目支持eslint+pritter+typescript的實現(xiàn)

 更新時間:2019年05月20日 10:32:48   作者:dodomonster  
這篇文章主要介紹了Nuxt項目支持eslint+pritter+typescript的實現(xiàn),小編覺得挺不錯的,現(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.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就會使用默認(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.tsbuild.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)文章

  • 詳解vue項目中調(diào)用百度地圖API使用方法

    詳解vue項目中調(diào)用百度地圖API使用方法

    這篇文章主要介紹了vue項目中調(diào)用百度地圖API使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • ElementUI中el-dropdown-item點擊事件無效問題

    ElementUI中el-dropdown-item點擊事件無效問題

    這篇文章主要介紹了ElementUI中el-dropdown-item點擊事件無效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue中組件傳值的常見方式小結(jié)

    vue中組件傳值的常見方式小結(jié)

    在 Vue.js 中,組件之間的數(shù)據(jù)傳遞是一個常見的需求,Vue 提供了多種方法來實現(xiàn)這一目標(biāo),包括 props、全局事件總線、消息的訂閱與發(fā)布等,下面我們就來學(xué)習(xí)一下這些方法的具體實現(xiàn)吧
    2023-12-12
  • 前端使用vue實現(xiàn)token無感刷新的三種方案解析

    前端使用vue實現(xiàn)token無感刷新的三種方案解析

    這篇文章主要為大家介紹了前端使用vue實現(xiàn)token無感刷新的三種方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • vue解決跨域問題的幾種方式

    vue解決跨域問題的幾種方式

    這篇文章主要給大家介紹了關(guān)于vue解決跨域問題的幾種方式,跨域跨域報錯是前端開發(fā)中非常經(jīng)典的一個錯誤,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • 詳解vue 數(shù)據(jù)傳遞的方法

    詳解vue 數(shù)據(jù)傳遞的方法

    這篇文章給大家介紹了vue數(shù)據(jù)傳遞的方法,包括,父組件向子組件傳值 ,子組件向父組件傳值 ,路由傳值等多種方法,需要的朋友參考下
    2018-04-04
  • 對vue生命周期的深入理解

    對vue生命周期的深入理解

    這篇文章主要給大家介紹了關(guān)于對vue生命周期的深入理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • vueJS簡單的點擊顯示與隱藏的效果【實現(xiàn)代碼】

    vueJS簡單的點擊顯示與隱藏的效果【實現(xiàn)代碼】

    下面小編就為大家?guī)硪黄獀ueJS簡單的點擊顯示與隱藏的效果【實現(xiàn)代碼】。小編覺得挺不錯的,現(xiàn)在分享給大家,一起跟隨小編過來看看吧
    2016-05-05
  • Vue3 響應(yīng)式 API 及 reactive 和 ref 的用法示例詳解

    Vue3 響應(yīng)式 API 及 reactive 和 ref&

    響應(yīng)式是一種允許以聲明式的方式去適應(yīng)變化的編程范例,這篇文章主要介紹了關(guān)于Vue3響應(yīng)式API及reactive和ref的用法,需要的朋友可以參考下
    2023-06-06
  • vue條件渲染列表渲染原理示例詳解

    vue條件渲染列表渲染原理示例詳解

    這篇文章主要為大家介紹了vue條件渲染列表渲染原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評論