Vue.js 安裝
1、獨立版本
我們可以在 Vue.js 的官網上直接下載 vue.min.js 並用 <script> 標簽引入。
2、使用 CDN 方法
以下推薦國外比較穩定的兩個 CDN,國內還沒發現哪一家比較好,目前還是建議下載到本地。
-
Staticfile CDN(國內) : https://cdn.staticfile.org/vue/2.2.2/vue.min.js
-
unpkg:https://unpkg.com/vue/dist/vue.js, 會保持和 npm 發布的最新的版本一致。
-
cdnjs : https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.min.js
3、NPM 方法
由於 npm 安裝速度慢,本教程使用了淘寶的鏡像及其命令 cnpm,安裝使用介紹參照:使用淘寶 NPM 鏡像。
npm 版本需要大於 3.0,如果低於此版本需要升級它:
# 查看版本 $ npm -v 2.3.0 #升級 npm cnpm install npm -g # 升級或安裝 cnpm npm install cnpm -g
在用 Vue.js 構建大型應用時推薦使用 cnpm 安裝:
# 最新穩定版 $ cnpm install vue
命令行工具
Vue.js 提供一個官方命令行工具,可用於快速搭建大型單頁應用。
# 全局安裝 vue-cli $ cnpm install --global vue-cli # 創建一個基於 webpack 模板的新項目 $ vue init webpack my-project # 這里需要進行一些配置,默認回車即可 This will install Vue 2.x version of the template. For Vue 1.x use: vue init webpack#1.0 my-project ? Project name my-project ? Project description A Vue.js project ? Author runoob <test@runoob.com> ? Vue build standalone ? Use ESLint to lint your code? Yes ? Pick an ESLint preset Standard ? Setup unit tests with Karma + Mocha? Yes ? Setup e2e tests with Nightwatch? Yes vue-cli · Generated "my-project". To get started: cd my-project npm install npm run dev Documentation can be found at https://vuejs-templates.github.io/webpack
進入項目,安裝並運行:
$ cd my-project $ cnpm install $ cnpm run dev DONE Compiled successfully in 4388ms > Listening at http://localhost:8080
成功執行以上命令后訪問 http://localhost:8080/,輸出結果如下所示:
注意:Vue.js 不支持 IE8 及其以下 IE 版本。
Vue 項目打包
打包 Vue 項目使用以下命令:
npm run build
執行完成后,會在 Vue 項目下生成一個 dist 目錄,一般包含 index.html 文件及 static 目錄,static 目錄包含了靜態文件 js、css 以及圖片目錄 images。
如果直接雙擊 index.html 打開瀏覽器,頁面可能是空白了,想要修改下 index.html 文件中 js、css 路徑即可。
例如我們打開 dist/index.html 文件看到路徑是絕對路徑:
<link href=/static/css/app.33da80d69744798940b135da93bc7b98.css rel=stylesheet> <script type=text/javascript src=/static/js/app.717bb358ddc19e181140.js></script>
我們把 js、css 路徑路徑修改為相對路徑:
<link href=static/css/app.33da80d69744798940b135da93bc7b98.css rel=stylesheet> <script type=text/javascript src=static/js/app.717bb358ddc19e181140.js></script>
這樣直接雙擊 dist/index.html 文件就可以在瀏覽器中看到效果了。