$ npm install --global vue-cli
html
$ vue init webpack vue-cli-demo
vue
cd vue-cli-demo
npm install
npm run dev
webpack
Tips:對於大陸用戶,建議將 npm 的註冊表源設置爲國內的鏡像,能夠大幅提高安裝速度,爲了保證npm與cnpm版本一致,建議在安裝(install)以前執行一次如下代碼web
alias cnpm="npm --registry=https://registry.npm.taobao.org \ --cache=$HOME/.npm/.cache/cnpm \ --disturl=https://npm.taobao.org/dist \ --userconfig=$HOME/.cnpmrc"
Tips:因爲最新的 vue-webpack-template
中已經去掉了 dev-server.js
改用 webpack-dev-server
代替,且默認再也不自動打開瀏覽器,須要自動打開瀏覽器的能夠在 config
文件下的index.js
文件中找到 dev
對象中的autoOpenBrowser
屬性,並將它的值由false
改成true
便可。vue-cli
vue-resource
:cnpm install vue-resource --save
express
:cnpm install express --save
build
文件夾下的webpack.dev.conf.js
文件,加入如下代碼,模擬實現後臺數據加載。const express = require('express') const bodyParser = require('body-parser') const fs = require('fs') var apiServer = express() var port = process.env.PORT || config.dev.port apiServer.use(bodyParser.urlencoded({ extended: true })) apiServer.use(bodyParser.json()) var apiRouter = express.Router() apiRouter.route('/:apiName').all(function (req, res) { fs.readFile('./db.json', 'utf8', function (err, data) { if (err) throw err var data = JSON.parse(data) if (data[req.params.apiName]) { res.json(data[req.params.apiName]) } else { res.send('err:no such api name') } }) }) apiServer.use('/api', apiRouter); apiServer.listen(port + 1, function (err) { if (err) { console.log(err) return } console.log('Listening at http://localhost:' + (port + 1) + '\n') })
config
文件下的index.js
文件,找到proxyTable:{}
,向{}
裏面加入代碼:'/api': 'http://localhost:8081'
實現數據代理。db.json
,在文件中寫入所需數據便可。Tips:在此項目中,咱們使用了默認端口port:8080
加載服務,且設置數據監聽端口爲port + 1
,所以,在數據代理代碼中,端口的數值爲8081
express
打開同步下來的官方腳手架項目文件中Router
文件夾下的index.js
文件。apache
path: '/', component: IndexPage, meta: { title: '首頁-售賣平臺' } }, { path: '/detail', component: DetailPage, redirect: '/detail/analysis', children: [{ path: 'analysis', component: DetailAnaPage, meta: { title: '流量分析-售賣平臺' } }, { path: 'count', component: DetailCouPage, meta: { title: '數據統計-售賣平臺' } }, { path: 'forecast', component: DetailForPage, meta: { title: '數據預測-售賣平臺' } }, { path: 'publish', component: DetailPubPage, meta: { title: '廣告發布-售賣平臺' } }, ] }, { path: '/orderList', component: OrderListPage, meta: { title: '訂單列表-售賣平臺' } } ];
const router = new VueRouter({ mode: 'history', //base: 'vue-cli', routes });
router.afterEach(route => { // 從路由的元信息中獲取 title 屬性 if (route.meta.title) { document.title = route.meta.title; // 若是是 iOS 設備,則使用以下 hack 的寫法實現頁面標題的更新 if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) { const hackIframe = document.createElement('iframe'); hackIframe.style.display = 'none'; hackIframe.src = '/robots.txt?r=' + Math.random(); document.body.appendChild(hackIframe); setTimeout(_ => { document.body.removeChild(hackIframe); }, 300); } } })
在代碼打包發佈以前,用戶能夠根據網上教程,本地自主安裝Apache服務器。服務安裝好以後,咱們須要將代碼放到咱們的服務器上,這就須要咱們將代碼打包發佈。執行 npm run build
命令,在咱們的項目目錄中會出現一個dist文件夾,這就是咱們須要放到服務器上發佈的文件。然而,每每咱們將打包好的文件放到服務器上時,網站卻不能正常打開,那是咱們的路由文件和配置文件路徑配置錯誤,須要從新編寫。npm
config
文件夾中的index.js
文件,找到build
對象中的assetsPublicPath
屬性,將其值設置爲'/'
(vue-cli腳手架的默認值即爲'/'
)base
屬性及其值。Apache
服務器安裝目錄下htdocs
文件夾內的文件,執行npm run build
命令,項目中會產生一個dist
文件夾,拷貝該文件夾下的index.html
和static
文件夾到htdocs
目錄下。Apache
服務器上路由跳轉是有問題的,須要在apache
的配置文件httpd.conf
裏面配置路由跳轉。如圖所示,在配置文件加入以下代碼:AllowOverride all RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L]
localhost:port
便可訪問該網站。config
文件夾中的index.js
文件,找到build
對象中的assetsPublicPath
屬性,將其值由默認值'/'
改成'/store/'
。再打開源碼目錄src
下的路由配置文件夾router
下的index.js
文件,在路由配置中添加base:'store'
。npm run build
命令,項目中會產生一個dist
文件夾,拷貝該文件夾到htdocs
目錄下。並將其名稱改成store
。Apache
服務器上路由跳轉是有問題的,須要在apache
的配置文件httpd.conf
裏面配置路由跳轉。如圖所示,在配置文件加入以下代碼:AllowOverride all RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /store/index.html [L]
localhost:port/store
便可訪問該網站。store
,在你的項目中你能夠自主命名,可是,在發佈過程當中全部用到該名稱的地方或位置,名稱必須保持一致。 config
文件夾中的index.js
文件,找到build
對象中的assetsPublicPath
屬性,將其值設置爲'/'
(vue-cli腳手架的默認值即爲'/'
)base
屬性及其值。npm run build
命令,項目中會產生一個dist
文件夾,拷貝或者剪切該文件夾到任意目錄(非Apache
安裝目錄),並進行重命名。本例咱們將該文件夾命名爲store
。apache
的配置文件httpd.conf
裏面打開虛擬主機Virtual hosts
,即:刪除Include conf/extra/httpd-vhosts.conf
這一行代碼前的#
號。conf
文件夾下extra
文件夾中的httpd-vhosts.conf
文件,在其中配置以下代碼:<VirtualHost *:80> ServerAdmin admin@code.com DocumentRoot "E:/store/" ServerName www.store.com ServerAlias www.store.com ErrorLog "logs/dummy-host.example.com-error.log" CustomLog "logs/dummy-host.example.com-access.log" common <Directory /> AllowOverride all RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] </Directory> </VirtualHost>其中,
ServerAdmin
表明網站管理員,DocumentRoot
表明打包好的項目文件拷貝另存的路徑,ServerName
表明Url連接,ServerAlias
表明連接別名。json
127.0.0.1 www.store.com
。www.store.com
便可訪問該網站。在咱們修改Apache服務器的配置文件的時候,RewriteRule
的值中的.
與後面的路徑中間有空格。api