了解如何使用Vue.js CLI

Interested in learning Vue.js? Get my ebook at vuehandbook.com

有興趣學習Vue.js嗎? 在vuehandbook.com上獲取我的電子書

One of them is the Vue Command Line Interface (CLI).

其中之一是Vue命令行界面(CLI)。

Note: There is a huge rework of the CLI going on right now, going from version 2 to 3. While not yet stable, I will describe version 3 because it’s a huge improvement over version 2, and quite different.

注意:從版本2到版本3,現在正在對CLI進行大量修改。雖然尚不穩定,但我將介紹版本3,因為它是對版本2的巨大改進,并且有很大的不同。

安裝 (Installation)

The Vue CLI is a command line utility, and you install it globally using npm:

Vue CLI是一個命令行實用程序,您可以使用npm在全局安裝它:

npm install -g @vue/cli

or using yarn:

或使用紗線:

yarn global add @vue/cli

Once you do so, you can invoke the vue command.

完成后,您可以調用vue命令。

Vue CLI提供了什么? (What does the Vue CLI provide?)

The CLI is essential for rapid Vue.js development.

CLI對于快速Vue.js開發至關重要。

Its main goal is to make sure all the tools you need are working along, to perform what you need. It abstracts away all the nitty-gritty configuration details that using each tool in isolation would require.

它的主要目標是確保所需的所有工具都可以正常工作,以執行所需的工作。 它抽象出了單獨使用每個工具所需的所有細節配置細節。

It can perform an initial project setup and scaffolding.

它可以執行初始項目設置和腳手架。

It’s a flexible tool. Once you create a project with the CLI, you can go and tweak the configuration, without having to eject your application (like you’d do with create-react-app). You can configure anything and still be able to upgrade with ease.

這是一個靈活的工具。 使用CLI創建項目后,就可以進行配置調整,而不必退出應用程序(就像對create-react-app )。 您可以進行任何配置,仍然可以輕松升級。

After you create and configure the app, it acts as a runtime dependency tool, built on top of webpack.

創建和配置應用程序后,它充當基于Webpack的運行時依賴工具。

The first encounter with the CLI is when creating a new Vue project.

與CLI的第一次接觸是在創建新的Vue項目時。

如何使用CLI創建新的Vue項目 (How to use the CLI to create a new Vue project)

The first thing you’re going to do with the CLI is to create a Vue app:

使用CLI要做的第一件事是創建一個Vue應用程序:

vue create example

The cool thing is that it’s an interactive process. You need to pick a preset. By default, there is one preset that’s providing Babel and ESLint integration:

最酷的是,這是一個交互式過程。 您需要選擇一個預設。 默認情況下,有一個預設提供Babel和ESLint集成:

I’m going to press the down arrow ?? and manually choose the features I want:

我將按向下箭頭??并手動選擇所需的功能:

Press space to on each feature you need, and then press enter to go on. Since I chose “Linter/Formatter”, Vue CLI prompts me for the configuration. I chose “ESLint + Prettier” since that’s my favorite setup:

space選擇所需的每個功能,然后按enter繼續。 由于我選擇了“ Linter / Formatter”,因此Vue CLI會提示我進行配置。 我選擇“ ESLint + Prettier”,因為這是我最喜歡的設置:

The next step is choosing how to apply linting. I chose “Lint on save”.

下一步是選擇如何應用棉絨。 我選擇了“保存時不掉毛”。

Next up: testing. I picked testing, and Vue CLI offers me the two most popular solutions to choose from: “Mocha + Chai” vs “Jest”.

接下來:測試。 我選擇了測試,Vue CLI為我提供了兩種最受歡迎??的解決方案供您選擇:“ Mocha + Chai”和“ Jest”。

Vue CLI asks me where to put all the configuration. The choices are in the “package.json” file, or in dedicated configuration files, one for each tool. I chose the latter.

Vue CLI詢問我將所有配置放在何處。 選項位于“ package.json”文件或專用配置文件中,每個工具一個。 我選擇了后者。

Next, Vue CLI asks me if I want to save these presets, and allows me to pick them as a choice the next time I use Vue CLI to create a new app. It’s a very convenient feature. Having a quick setup with all my preferences is a complexity reliever:

接下來,Vue CLI詢問我是否要保存這些預設,并允許我下次使用Vue CLI創建新應用時選擇它們。 這是一個非常方便的功能。 快速設置所有偏好設置可以緩解復雜性:

Vue CLI then asks me if I prefer using yarn or npm:

然后,Vue CLI詢問我是否更喜歡使用yarn或npm:

and it’s the last thing it asks me. It then it goes on to download the dependencies and create the Vue app:

這是我要問的最后一件事。 然后繼續下載依賴項并創建Vue應用程序:

如何啟動新創建的Vue CLI應用程序 (How to start the newly created Vue CLI application)

Vue CLI has created the app for us, and we can go in the “example” folder and run yarn serve to start up our first app in development mode:

Vue CLI已經為我們創建了應用程序,我們可以進入“ example”文件夾并運行yarn serve以開發模式啟動我們的第一個應用程序:

The starter example application source contains a few files, including “package.json”:

入門示例應用程序源包含一些文件,包括“ package.json”:

This is where all the CLI commands are defined, including yarn serve, which we used a minute ago. The other commands are

這是定義所有CLI命令的地方,包括我們在一分鐘前使用的yarn serve 。 其他命令是

  • yarn build, to start a production build

    yarn build ,開始生產

  • yarn lint, to run the linter

    yarn lint

  • yarn test:unit, to run the unit tests

    yarn test:unit ,運行單元測試

I will describe the sample application generated by Vue CLI in a separate tutorial.

我將在單獨的教程中描述由Vue CLI生成的示例應用程序。

Git倉庫 (Git repository)

Notice the master word in the lower-left corner of VS Code? That’s because Vue CLI automatically creates a repository, and makes a first commit. We can jump right in, change things, and we know what we changed:

注意VS Code左下角的master詞嗎? 這是因為Vue CLI自動創建存儲庫并進行第一次提交。 我們可以直接進入,進行更改,我們知道更改了什么:

This is pretty cool. How many times do you dive in and change things only to realize, when you want to commit the result, that you didn’t commit the initial state?

這很酷。 當您要提交結果時,您潛入了多少次并進行更改以僅意識到自己沒有提交初始狀態?

從命令行使用預設 (Use a preset from the command line)

You can skip the interactive panel and instruct Vue CLI to use a particular preset:

您可以跳過交互式面板,并指示Vue CLI使用特定的預設:

vue create -p favourite example-2

預設存儲在哪里 (Where presets are stored)

Presets are stored in the “.vuejs” file in your home directory. Here’s mine after creating the first “favourite” preset:

預設存儲在主目錄中的“ .vuejs”文件中。 創建第一個“收藏夾”預設后,這是我的:

{  "useTaobaoRegistry": false,  "packageManager": "yarn",  "presets": {    "favourite": {      "useConfigFiles": true,      "plugins": {        "@vue/cli-plugin-babel": {},        "@vue/cli-plugin-eslint": {          "config": "prettier",          "lintOn": [            "save"          ]        },        "@vue/cli-plugin-unit-jest": {}      },      "router": true,      "vuex": true    }  }}

外掛程式 (Plugins)

As you can see from reading the configuration, a preset is basically a collection of plugins, with some optional configuration.

從閱讀配置可以看到,預設基本上是插件的集合,帶有一些可選配置。

Once a project is created, you can add more plugins by using vue add:

創建項目后,您可以使用vue add添加更多插件:

vue add @vue/cli-plugin-babel

All those plugins are used at the latest version available. You can force Vue CLI to use a specific version by passing the version property:

所有這些插件均以可用的最新版本使用。 您可以通過傳遞version屬性來強制Vue CLI使用特定版本:

"@vue/cli-plugin-eslint": {  "version": "^3.0.0"}

This is useful if a new version has breaking changes or a bug, and you need to wait a little bit before using it.

如果新版本具有重大更改或錯誤,并且您需要稍等片刻才能使用它,這很有用。

遠程存儲預設 (Remotely store presets)

A preset can be stored in GitHub (or on other services) by creating a repository that contains a “preset.json” file, which contains a single preset configuration.

通過創建包含“ preset.json”文件的存儲庫,可以將預置存儲在GitHub(或其他服務)中,該文件包含一個預置配置。

Extracted from the above, I made a sample preset in https://github.com/flaviocopes/vue-cli-preset/blob/master/preset.json which contains this configuration:

從上面提取的內容,我在https://github.com/flaviocopes/vue-cli-preset/blob/master/preset.json中進行了一個示例預置,其中包含以下配置:

{  "useConfigFiles": true,  "plugins": {    "@vue/cli-plugin-babel": {},    "@vue/cli-plugin-eslint": {      "config": "prettier",      "lintOn": [        "save"      ]    },    "@vue/cli-plugin-unit-jest": {}  },  "router": true,  "vuex": true}

It can be used to bootstrap a new application using:

可以使用以下命令來引導新應用程序:

vue create --preset flaviocopes/vue-cli-preset example3

Vue CLI的另一種用法:快速原型制作 (Another use of the Vue CLI: rapid prototyping)

Until now, I’ve explained how to use the Vue CLI to create a new project from scratch, with all the bells and whistles. But for really quick prototyping, you can create a really simple Vue application — one that’s self-contained in a single .vue file — and serve that, without having to download all the dependencies in the node_modules folder.

到目前為止,我已經解釋了如何使用Vue CLI從頭開始創建所有項目。 但是,要真正快速地進行原型制作,您可以創建一個非常簡單的Vue應用程序-一個獨立包含在單個.vue文件中的應用程序-并為其提供服務,而不必下載node_modules文件夾中的所有依賴node_modules

How? First install the cli-service-global global package:

怎么樣? 首先安裝cli-service-global全局軟件包:

npm install -g @vue/cli-service-global
//or yarn
global add @vue/cli-service-global

Create an “app.vue” file:

創建一個“ app.vue”文件:

<template>    <div>        <h2>Hello world!</h2>        <marquee>Heyyy<;/marquee>    </div></template>

and then run

然后運行

vue serve app.vue

You can serve more organized projects, composed by JavaScript and HTML files as well. Vue CLI by default uses “main.js” / “index.js” as its entry point. You can have a “package.json” and any tool configuration set up. vue serve will pick it up.

您還可以提供由JavaScript和HTML文件組成的更有條理的項目。 默認情況下,Vue CLI使用“ main.js” /“ index.js”作為其入口點。 您可以設置“ package.json”和任何工具配置。 vue serve會撿起來。

Since this uses global dependencies, it’s not an optimal approach for anything more than demonstration or quick testing.

由于這使用了全局依賴關系,因此除了演示或快速測試之外,它不是最佳方法。

Running the vue build command will prepare the project for deployment, and generate the resulting JavaScript files in the dist/ folder, so that it will be production-ready. All the warnings that Vue.js generates during development are removed, and the code is optimized for real-world usage.

運行vue build命令將為項目做好部署準備,并在dist/文件夾中生成生成JavaScript文件,以便將其投入生產。 Vue.js在開發過程中生成的所有警告均已刪除,并且代碼已針對實際使用進行了優化。

Webpack (Webpack)

Internally, Vue CLI uses Webpack, but the configuration is abstracted and we don’t even see the config file in our folder. You can still access it by calling vue inspect:

在內部,Vue CLI使用Webpack,但是配置是抽象的,我們甚至在文件夾中都看不到配置文件。 您仍然可以通過調用vue inspect來訪問它:

Interested in learning Vue.js? Get my ebook at vuehandbook.com

有興趣學習Vue.js嗎? 在vuehandbook.com上獲取我的電子書

翻譯自: https://www.freecodecamp.org/news/learn-how-to-use-the-vue-js-cli-8349fb23a566/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393817.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393817.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393817.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

rabbitmq學習——隊列

public class Send { public static final String routingKey "wuqidi_task_durable"; /*工作隊列 也叫任務隊列 目的是將任務發送到隊列中 由工作者進行處理 在后臺的多個工作者中 任務是共享的*/ public static void main(String[] args) throws Exception{ Conne…

python 戰艦_簡單Python戰艦

我最近開始學習python&#xff0c;并決定嘗試制作我的第一個項目。我正在嘗試做一個戰艦游戲&#xff0c;隨機放置兩個3塊長的船在一塊板上。但效果不太好。我為2號飛船做了一個臨時的循環&#xff0c;它應該檢查一下旁邊的兩個空間是否空閑&#xff0c;然后在那里建立自己的空…

leetcode面試題 08.12. 八皇后(回溯)

設計一種算法&#xff0c;打印 N 皇后在 N N 棋盤上的各種擺法&#xff0c;其中每個皇后都不同行、不同列&#xff0c;也不在對角線上。這里的“對角線”指的是所有的對角線&#xff0c;不只是平分整個棋盤的那兩條對角線。 注意&#xff1a;本題相對原題做了擴展 示例: 輸…

linux 進入redis 數據庫,Linux下Redis數據庫的安裝方法與自動啟動腳本分享

安裝Redis(1) 下載Rediswget http://redis.googlecode.com/files/redis-2.2.11.tar.gztar xzvf redis-2.2.11.tar.gz(2) 編譯并安裝Redismake && make install(3) 復制并修改配置文件cp redis.conf /etc/redis.confvi /etc/redis.conf注意修改以下幾項&#xff1a;daem…

Flutter 36: 圖解自定義 View 之 Canvas (三)

小菜繼續學習 Canvas 的相關方法&#xff1a; drawVertices 繪制頂點 小菜上次沒有整理 drawVertices 的繪制方法&#xff0c;這次補上&#xff1b;Vertice 即頂點&#xff0c;通過繪制多個頂點&#xff0c;在進行連線&#xff0c;多用于 3D 模型中&#xff1b; drawVertices 包…

sphinx 項目根目錄_如何使用Sphinx工具記錄Django項目

sphinx 項目根目錄I recently visited a company where I had a nice talk with one of its employees. We talked about technology and programming. Then we touched the subject of project documentation. Specifically how React does it automatically but Django doesn…

程序員必知之浮點數運算原理詳解

導讀&#xff1a;浮點數運算是一個非常有技術含量的話題&#xff0c;不太容易掌握。許多程序員都不清楚使用操作符比較float/double類型的話到底出現什么問題。 許多人使用float/double進行貨幣計算時經常會犯錯。這篇文章是這一系列中的精華&#xff0c;所有的軟件開發人員都應…

axure選中后橫線切換_3、開關狀態切換 —— Axure實用交互

寫在開頭:開關的制作在幾乎所有原型設計中都會用到&#xff0c;所以美觀自然的交互開關可以給你的原型設計加分不少。本次開關設計主要用到的是邏輯為&#xff1a;選中狀態的切換首先&#xff0c;來看一下演示動畫開始原型設計一、創建元件首先需要打開Axure軟件&#xff0c;并…

Django框架——模型(數據庫操作)

-- models.py-- ORM(object-relation mapping) 實現數據模型與數據庫的解耦&#xff1b;# 對象&#xff0c;關系&#xff0c;映射&#xff1b;1.根 據對象的類型生成表結構&#xff1b;2.將對象、列表的操作&#xff0c;轉換為sql語句&#xff1b;3.將sql查詢到的結果轉換為對象…

leetcode140. 單詞拆分 II(回溯+記憶化)

給定一個非空字符串 s 和一個包含非空單詞列表的字典 wordDict&#xff0c;在字符串中增加空格來構建一個句子&#xff0c;使得句子中所有的單詞都在詞典中。返回所有這些可能的句子。 說明&#xff1a; 分隔時可以重復使用字典中的單詞。 你可以假設字典中沒有重復的單詞。 …

#loj 3058 [HNOI2019] 白兔之舞

單位根反演思博題 模數是亂給的記得整個任意模數ntt k為p-1的約數意味著一定存在k次單位根&#xff0c;設g是p的原根則\(w_{k}^{1}g^{\frac{k-1}{p}}\) 既然k次單位根存在自然考慮單位根反演了 設\(f(i)\)表示跳了i步并且停在了第二維為y的頂點的方案數 設\(st\)表示初始向量而…

標桿徐2018 Linux自動化運維實戰,標桿徐2018 Linux自動化運維系列⑦: SaltStack自動化配置管理實戰...

結合企業自動化集群場景講解&#xff0c;輕松玩轉SaltStack自動化配置管理工具第1章 SaltStack基礎應用SaltStack安裝SaltStack認證Saltstack遠程執行SaltStack配置管理第2章 SaltStack數據系統SaltStack數據系統-Grains 客戶端向服務端發送狀態SaltStack數據系統-paiil 服務…

JS 對象引用問題

var a {n:1}; var b a; a {n:2}; a.x a ;console.log(a.x);console.log(b.x); var a {n:1}; var b a; a.x a {n:2}; console.log(a.x);console.log(b.x); 這兩個問題主要理解兩點就很簡單了。 對象是引用類型&#xff0c;改變賦值只是改變指針的引用。運算符相當于改變…

工程代碼_Egret開發筆記(二)基礎工程代碼閱讀

代碼目錄結構在Egret Wing中打開上一節中我們創建的項目工程&#xff0c;查看代碼目錄結構&#xff0c;Forward在如下圖中標記了各個目錄的及關鍵文件的用途。代碼閱讀理解接下來我們從web入口一步一步閱讀初始代碼。首先打開index.html文件&#xff0c;我們看到index文件內容如…

知曉云助力小程序開發

小程序開發遇到瓶頸雖然騰訊提供了小程序解決方案&#xff0c;https://cloud.tencent.com/solution/la。但是對于普通開發者或者小企業的開發人員來說&#xff0c;購買域名&#xff0c;網站備案、部署SSL證書&#xff0c;安裝會話服務器。業務邏輯上要使用數據庫&#xff0c;緩…

leetcode131. 分割回文串(回溯)

給定一個字符串 s&#xff0c;將 s 分割成一些子串&#xff0c;使每個子串都是回文串。 返回 s 所有可能的分割方案。 示例: 輸入: “aab” 輸出: [ [“aa”,“b”], [“a”,“a”,“b”] ] 代碼 class Solution {List<List<String>> stringListnew ArrayList…

Cracer滲透-windows基礎(系統目錄,服務,端口,注冊表)

系統目錄C:\Windows\system32\config\SAM (保存系統密碼) 無法正常修改&#xff0c;可以進入PE系統進行修改&#xff08;先備份在清空&#xff09;利用結束后&#xff0c;再將之前備份的恢復C:\Windows\System32\drivers\hosts&#xff08;域名解析文件&#xff09;hosts欺騙&a…

java--xml文件讀取(SAX)

SAX解析原理&#xff1a; 使用Handler去逐個分析遇到的每一個節點 SAX方式解析步奏&#xff1a; 創建xml解析需要的handler&#xff08;parser.parse(file,handler)&#xff09; package com.imooc_xml.sax.handler;import java.util.ArrayList;import org.xml.sax.Attributes…

算法訓練營 重編碼_編碼訓練營之后該做什么-以及如何獲得成功

算法訓練營 重編碼by Anthony Morris安東尼莫里斯(Anthony Morris) 編碼訓練營之后該做什么-以及如何獲得成功 (What to do — and how to find success — after a coding bootcamp) It’s almost been two years since I graduated from the Lighthouse Labs Web Developmen…

leetcode860. 檸檬水找零(貪心)

在檸檬水攤上&#xff0c;每一杯檸檬水的售價為 5 美元。 顧客排隊購買你的產品&#xff0c;&#xff08;按賬單 bills 支付的順序&#xff09;一次購買一杯。 每位顧客只買一杯檸檬水&#xff0c;然后向你付 5 美元、10 美元或 20 美元。你必須給每個顧客正確找零&#xff0…