vue-cli 3.0安裝和使用

 

零. 前言

    公司最近開發項目使用的是vue-cli 3.0版本開發,可是對於vue-cli 3.0版本一直沒有研究過如何使用,公司使用配置:pug + ts + stylus + eslint;編輯器使用:vscode,使用起來簡直不要太爽。css

默認你已經安裝了nodejs前端

一.安裝vue-cli 3.0

1.安裝:vue

npm install -g @vue/cli
  • -g: 全局安裝 vue-cli

二.建立項目

1.建立vue-app項目:node

vue create vue-app

2.項目配置:vue-cli

在這裏插入圖片描述

  • 默認配置
  • 手動配置:babel ts 預編譯 等等… 【選擇這個】

如下是我選擇的配置(能夠直接按數字鍵1,2,3,4進行選擇)typescript

  • Babel:將ES6編譯成ES5
  • TypeScript:JS超集,主要是類型檢查
  • RouterVuex,路由和狀態管理
  • Linter/ Formatter:代碼檢查工具
  • CSS Pre-processors:css預編譯 (稍後會對這裏進行配置)
  • Unit Testing:單元測試,開發過程當中前端對代碼進行自運行測試

Use class-style component syntax? (Y/n) y

是否使用Class風格裝飾器?
即本來是:home = new Vue()建立vue實例
使用裝飾器後:class home extends Vue{}npm


Use Babel alongside TypeScript for auto-detected polyfills? (Y/n) y

使用Babel與TypeScript一塊兒用於自動檢測的填充? yesjson


Use history mode for router? (Requires proper server setup for index fallback in production) (Y/n) y

路由使用歷史模式? 這種模式充分利用 history.pushState API 來完成 URL 跳轉而無須從新加載頁面babel


在這裏插入圖片描述

使用什麼css預編譯器? 我選擇的 stylusapp


在這裏插入圖片描述

  • tslint: typescript格式驗證工具
  • eslint w...: 只進行報錯提醒; 【選這個】
  • eslint + A...: 不嚴謹模式;
  • eslint + S...: 正常模式;
  • eslint + P...: 嚴格模式;

在這裏插入圖片描述
代碼檢查方式:我選擇保存時檢查


![在這裏插入圖片描述](https://img-blog.csdn.net/20181017222800822?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXdlbndlbjY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

單元測試工具,這裏附上vue單元測試的連接,想了解的小夥伴戳這裏 https://vue-test-utils.vuejs.org/zh/


在這裏插入圖片描述

vue-cli 通常來說是將全部的依賴目錄放在package.json文件裏


Save this as a preset for future projects? (y/N) n

是否在之後的項目中使用以上配置?不


在這裏插入圖片描述

下載依賴的工具:使用 yarn,速度快。

到此爲止,安裝就完成了,能夠等待安裝成功。

三.使用

1.vscodeeslint配置,使代碼可以在ctrl+s的時候自動格式化:
vue-app目錄縣新建文件夾.vscode文件,再在.vscode目錄下新建settings.json,文件內容以下:

tips:此配置包含了.vue文件.styl文件typescript的代碼縮進

{
    "prettier.printWidth": 160,
    "prettier.tabWidth": 2,
    "prettier.semi": false,
    "prettier.singleQuote": true,
    "prettier.disableLanguages": [],

    "editor.tabSize": 2,
    "[vue]": {
        "editor.formatOnSave": true
    },
    "[typescript]": {
        "editor.formatOnSave": true,
    },
    "[stylus]": {
        "editor.formatOnSave": true,
    },

    "stylusSupremacy.insertColons": false,
    "stylusSupremacy.insertSemicolons": false,
    "stylusSupremacy.insertBraces": false,

    "languageStylus.useSeparator": false,
}
  • 這樣作:
    表示vscode在讀取到vue-app項目是,回去查找.vscode下的settings.json配置並應用。必須禁用插件:eslint

2.使用pug

yarn add pug pug-plain-loader --dev
  • 使用yarn安裝 pugpug-plain-loader;(沒有yarn的自行百度安裝
  • --dev:安裝到開發環境
  • 使用:打開App.vue文件,將文件修改成下面這樣既可。
<template lang="pug">
  #app
    #nav
      router-link(to="/") Home
      router-link(to="/about") About
    router-view
  </div>
</template>

4、常見的一些問題

0.其餘一些eslint配置均可以在packge.json文件中的eslintConfig下的rules下配置

1.console.log(1)報錯:
找到packge.json文件中的eslintConfig下的rules

"no-console": "off"

參考文獻:

安裝
eslint配置

相關文章
相關標籤/搜索