Vite+Vue3+TS 初始化項目

項目初始化

項目介紹: 本項目使用vite2.7.0和vue3.2.23 和 Typescript4.4.4(2021年12月初)最新的版本來實現后臺管理項目。并會使用element-plus做UI庫,axios做數據交互,vuex做狀態管理,vue-router做路由管理, ESlint+Prettier做代碼校驗和格式化, Echarts做頁面圖表可視化, less來做css預編譯。

初始化源碼地址github

1. 準備工作

node -v # 14.17.5 要求要 node 12版本以上
npm -v
yarn -v # 1.22.17 當前最新版
本項目使用 **vscode**作為主編譯器[vscode使用](http://www.lxweimin.com/p/8e127a3aba55)

2. 初始化

  1. 參考vite的文檔create-vite來做init: vite文檔地址
yarn create vite my-demo-app --template vue-ts # 直接初始化vite+vue+ts
cd my-demo-app
yarn
yarn dev # 能看到localhost:3000的默認歡迎頁面則初始化成功

此時的vite.config.js和tsconfig.js文件均已初始化默認選項。

vite.config.js常用配置

tsconfig.json常用配置

啟動項目不能通過本地ip地址訪問:解決辦法

  1. 引用UI庫:element-plus

    yarn add element-plus
    

    使用方法更多方式參考:更多用法

    // 這里僅展示完整引入
    // 修改 main.ts文件
    import { createApp } from 'vue'
    import ElementPlus from 'element-plus'
    import 'element-plus/dist/index.css'
    import App from './App.vue'
    createApp(App).use(ElementPlus, { size: 'small', zIndex: 100 }).mount('#app')
    
    <el-button type="primary"> 按鈕 </el-button> /*如果頁面上能看到藍色的按鈕,就說明引用成功了*/
    

    使用方式幾乎和element-ui保持一致

  2. 使用 Less來做css預編譯

yarn add less less-loader -D

然后再組件中使用less測試是否成功

<style scoped lang="less">
.text {
  color: red;
}
</style>
  1. 修改一些默認目錄結構,這個也可以后面什么時候用到什么時候再定義。

3. 使用 ESlint + Prettier 做代碼校驗和格式化

    哪里不能用校驗參考禁用[校驗方法](http://www.lxweimin.com/p/bb590f8e1c98)
  1. 引入eslint
yarn add eslint -D
yarn add @typescript-eslint/eslint-plugin -D
yarn add @typescript-eslint/parser -D
yarn add eslint-config-alloy -D
yarn add eslint-config-prettier -D
yarn add eslint-plugin-prettier -D
yarn add eslint-plugin-vue -D
yarn add vue-eslint-parser -D
yarn add vite-plugin-eslint -D
  1. 配置eslint, 在根目錄創建.eslintrc.js文件,并寫入以下內容:

     module.exports = {
       root: true,
       globals: {
         defineEmits: 'readonly',
         defineProps: 'readonly',
         ElMessage: 'readonly',
         ElMessageBox: 'readonly'
       },
       extends: ['plugin:@typescript-eslint/recommended', 'plugin:vue/vue3-recommended', 'airbnb-base'],
       parserOptions: {
         parser: '@typescript-eslint/parser',
         ecmaVersion: 2020
       },
       rules: {
         'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', // 禁用 debugger
         'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', // 禁用 console
         'no-bitwise': 'off', // 禁用按位運算符
         'no-tabs': 'off', // 禁用 tab
         'array-element-newline': ['error', 'consistent'], // 強制數組元素間出現換行
         indent: ['error', 2, { MemberExpression: 0, SwitchCase: 1, ignoredNodes: ['TemplateLiteral'] }], // 強制使用一致的縮進
         quotes: ['error', 'single'], // 強制使用一致的反勾號、雙引號或單引號
         'comma-dangle': ['error', 'always-multiline'], // 要求或禁止末尾逗號
         'object-curly-spacing': ['error', 'always'], // 強制在大括號中使用一致的空格
         'max-len': ['error', 120], // 強制一行的最大長度
         'no-new': 'off', // 禁止使用 new 以避免產生副作用
         'linebreak-style': 'off', // 強制使用一致的換行風格
         'import/extensions': 'off', // 確保在導入路徑中統一使用文件擴展名
         'eol-last': 'off', // 要求或禁止文件末尾存在空行
         'no-shadow': 'off', // 禁止變量聲明與外層作用域的變量同名
         'no-unused-vars': 'warn', // 禁止出現未使用過的變量
         'import/no-cycle': 'off', // 禁止一個模塊導入一個有依賴路徑的模塊回到自己身上
         'arrow-parens': 'off', // 要求箭頭函數的參數使用圓括號
         semi: ['error', 'never'], // 要求或禁止使用分號代替 ASI
         eqeqeq: 'on', // 要求使用 === 和 !==
         'no-param-reassign': 'off', // 禁止對 function 的參數進行重新賦值
         'import/prefer-default-export': 'off', // 如果模塊只輸入一個名字,則傾向于默認輸出
         'no-use-before-define': 'on', // 禁止在變量定義之前使用它們,則傾向于默認輸出
         'no-continue': 'off', // 禁用 continue 語句
         'prefer-destructuring': 'off', // 優先使用數組和對象解構
         'no-plusplus': 'off', // 禁用一元操作符 ++ 和 --
         'prefer-const': 'warn', // 要求使用 const 聲明那些聲明后不再被修改的變量
         'global-require': 'off', // 要求 require() 出現在頂層模塊作用域中
         'no-prototype-builtins': 'off', // 禁止直接調用 Object.prototypes 的內置屬性
         'consistent-return': 'off', // 要求 return 語句要么總是指定返回的值,要么不指定
         'one-var-declaration-per-line': 'off', // 要求或禁止在變量聲明周圍換行
         'one-var': 'off', // 強制函數中的變量要么一起聲明要么分開聲明
         'import/named': 'off', // 確保命名導入與遠程文件中的命名導出相對應
         'object-curly-newline': 'off', // 強制大括號內換行符的一致性
         'default-case': 'off', // 要求 switch 語句中有 default 分支
         'no-trailing-spaces': 'on', // 禁用行尾空格
         'func-names': 'off', // 要求或禁止使用命名的 function 表達式
         radix: 'off', // 強制在 parseInt() 使用基數參數
         'no-unused-expressions': 'off', // 禁止出現未使用過的表達式
         'no-underscore-dangle': 'off', // 禁止標識符中有懸空下劃線
         'no-nested-ternary': 'off', // 禁用嵌套的三元表達式
         'no-restricted-syntax': 'off', // 禁用特定的語法
         'no-await-in-loop': 'off', // 禁止在循環中出現 await
         'import/no-extraneous-dependencies': 'off', // 禁止使用外部包
         'import/no-unresolved': 'off', // 確保導入指向一個可以解析的文件/模塊
         'template-curly-spacing': ['error', 'always'], // 要求或禁止模板字符串中的嵌入表達式周圍空格的使用
         '@typescript-eslint/no-var-requires': 'on', // 除import語句外,禁止使用require語句
         '@typescript-eslint/no-empty-function': 'off', // 不允許空函數
         '@typescript-eslint/no-explicit-any': 'off', // 禁止使用 any 類型
         'guard-for-in': 'off', // 要求 for-in 循環中有一個 if 語句
         'class-methods-use-this': 'off', // 強制類方法使用 this
         'vue/html-indent': ['error', 2], // 在<template>中強制一致縮進
         'vue/html-self-closing': 'off', // 執行自閉合的風格
         'vue/max-attributes-per-line': [
           // 強制每行屬性的最大數量
           'warn',
           {
             singleline: {
               max: 5,
               allowFirstLine: true
             },
             multiline: {
               max: 1,
               allowFirstLine: false
             }
           }
         ],
         'vue/singleline-html-element-content-newline': 'off' // 要求單行元素的內容前后有一個換行符
       }
     }
    
    
  2. 配置 .eslintignore文件

    /build/
    /config/
    /dist/
    /*.js
    /*.zip
    /*.rar
    /node_modules/
    /test/unit/coverage/
    
    
  3. 引入Prettier來自動格式化代碼

    yarn add prettier -D
    
  4. 配置 Prettier, 在根目錄下創建 prettier.config.js文件,并寫入以下內容:

    // prettier.config.js or .prettierrc.js
    module.exports = {
      // 一行最多 100 字符
      printWidth: 100,
      // 使用 2 個空格縮進
      tabWidth: 2,
      // 不使用縮進符,而使用空格
      useTabs: false,
      // 行尾需要有分號
      semi: false,
      // 使用單引號
      singleQuote: true,
      // 對象的 key 僅在必要時用引號
      quoteProps: 'as-needed',
      // jsx 不使用單引號,而使用雙引號
      jsxSingleQuote: false,
      // 末尾不需要逗號
      trailingComma: 'none',
      // 大括號內的首尾需要空格
      bracketSpacing: true,
      // jsx 標簽的反尖括號需要換行
      jsxBracketSameLine: false,
      // 箭頭函數,只有一個參數的時候,也需要括號
      arrowParens: 'always',
      // 每個文件格式化的范圍是文件的全部內容
      rangeStart: 0,
      rangeEnd: Infinity,
      // 不需要寫文件開頭的 @prettier
      requirePragma: false,
      // 不需要自動在文件開頭插入 @prettier
      insertPragma: false,
      // 使用默認的折行標準
      proseWrap: 'preserve',
      // 根據顯示樣式決定 html 要不要折行
      htmlWhitespaceSensitivity: 'css',
      // 換行符使用 lf
      endOfLine: 'lf'
    }
    
    
  5. 對應的vscode編譯器也要安裝 ESLintPrettier 擴展

  6. 編輯 vscode 編輯器的settings.json文件。 ctrl+shift+p打開命令: Open Workspace Settings(JSON)選項,就會在根目錄出現一個.vscode的文件夾,修改里面的 settings.json文件如下:

{
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.tabSize": 2
}

此時可以代碼自動格式化啦!

初始化源碼地址[github](Lzq811/vite-vue-ts-eslint at vite2+vue3+ts4+eslint項目初始化 (github.com))

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。