$ 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