1. 在 config/index.js 配置文件中配置proxyTable
'use strict'
= require('path'module.exports =<span style="color: #000000"> {
dev: {
</span><span style="color: #008000">//</span><span style="color: #008000"> Paths</span>
assetsSubDirectory: 'static'<span style="color: #000000">,assetsPublicPath: </span>'/'<span style="color: #000000">,<span style="color: #ff0000"> proxyTable: {
</span></span><span style="color: #ff0000">'/apis': {
// 测试环境
target: 'http://www.thenewstep.cn/',// 接口域名
changeOrigin: true,//是否跨域
pathRewrite: {
'^/apis': '' //需要rewrite重写的,}
}
},</span><span style="color: #008000">//</span><span style="color: #008000"> Various Dev Server settings</span>
host: 'localhost',<span style="color: #008000">//</span><span style="color: #008000"> can be overwritten by process.env.HOST</span>
port: 8080,<span style="color: #008000">//</span><span style="color: #008000"> can be overwritten by process.env.PORT,if port is in use,a free one will be determined</span>
autoOpenBrowser: <span style="color: #0000ff">false</span><span style="color: #000000">,errorOverlay: </span><span style="color: #0000ff">true</span><span style="color: #000000">,notifyOnErrors: </span><span style="color: #0000ff">true</span><span style="color: #000000">,poll: </span><span style="color: #0000ff">false</span>,<span style="color: #008000">//</span><span style="color: #008000"> https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-</span>
<span style="color: #008000">/*</span><span style="color: #008000">*
* Source Maps
</span><span style="color: #008000">*/</span>
<span style="color: #008000">//</span><span style="color: #008000"> https://webpack.js.org/configuration/devtool/#development</span>
devtool: 'cheap-module-eval-source-map'<span style="color: #000000">,</span><span style="color: #008000">//</span><span style="color: #008000"> If you have problems debugging vue-files in devtools,</span>
<span style="color: #008000">//</span><span style="color: #008000"> set this to false - it *may* help</span>
<span style="color: #008000">//</span><span style="color: #008000"> https://vue-loader.vuejs.org/en/options.html#cachebusting</span>
cacheBusting: <span style="color: #0000ff">true</span><span style="color: #000000">,cssSourceMap: </span><span style="color: #0000ff">true</span><span style="color: #000000">
},build: {
<span style="color: #008000">//<span style="color: #008000"> Template for index.html
index: path.resolve(dirname,'../dist/index.html'<span style="color: #000000">),<span style="color: #008000">//<span style="color: #008000"> Paths
assetsRoot: path.resolve(dirname,'../dist'<span style="color: #000000">),assetsSubDirectory: 'static'<span style="color: #000000">,<span style="color: #008000">/
<span style="color: #008000">
- Source Maps
<span style="color: #008000">*/<span style="color: #000000">
productionSourceMap: </span><span style="color: #0000ff">true</span><span style="color: #000000">,</span><span style="color: #008000">//</span><span style="color: #008000"> https://webpack.js.org/configuration/devtool/#production</span>
devtool: '#source-map'<span style="color: #000000">,</span><span style="color: #008000">//</span><span style="color: #008000"> Gzip off by default as many popular static hosts such as</span>
<span style="color: #008000">//</span><span style="color: #008000"> Surge or Netlify already gzip all static assets for you.</span>
<span style="color: #008000">//</span><span style="color: #008000"> Before setting to `true`,make sure to:</span>
<span style="color: #008000">//</span><span style="color: #008000"> npm install --save-dev compression-webpack-plugin</span>
productionGzip: <span style="color: #0000ff">false</span><span style="color: #000000">,productionGzipExtensions: [</span>'js','css'<span style="color: #000000">],</span><span style="color: #008000">//</span><span style="color: #008000"> Run the build command with an extra argument to</span>
<span style="color: #008000">//</span><span style="color: #008000"> View the bundle analyzer report after build finishes:</span>
<span style="color: #008000">//</span><span style="color: #008000"> `npm run build --report`</span>
<span style="color: #008000">//</span><span style="color: #008000"> Set to `true` or `false` to always turn it on or off</span>
<span style="color: #000000"> bundleAnalyzerReport: process.env.npm_config_report
}
}
<span style="color: #008000">//
<span style="color: #008000">2. 行为:处理逻辑
<span style="color: #008000">//<span style="color: #008000">3. 样式:解决样式
import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import App from './App'
import Users from './components/Users'
import Home from './components/Home'
<span style="color: #008000">//
<span style="color: #008000">全局使用axios
<span style="color: #ff0000">Vue.prototype.$axios = axios
<span style="color: #008000">//<span style="color: #008000">设置token
<span style="color: #ff0000">axios.defaults.headers.common['token'] = "f4c902c9ae5a2a9d8f84868ad064e706"
<span style="color: #008000">//<span style="color: #008000">设置请求头
<span style="color: #ff0000">axios.defaults.headers.post["Content-type"] = "application/json"<span style="color: #000000">
Vue.config.productionTip
= <span style="color: #0000ff">false
<span style="color: #008000">//<span style="color: #008000">引入路由后使用路由,这样就可以在任何组件中使用路由了
<span style="color: #000000">Vue.use(VueRouter)
<span style="color: #008000">//<span style="color: #008000">引入vue-resource后使用vue-resource,这样就可以在任何组件中使用http了
<span style="color: #000000">Vue.use(VueResource)
<span style="color: #008000">//<span style="color: #008000">配置路由
const router = <span style="color: #0000ff">new<span style="color: #000000"> VueRouter(
{
routes : [
<span style="color: #008000">//<span style="color: #008000">配置路由跳转到Home这个组件
{path:"/"<span style="color: #000000">,component:Home},<span style="color: #008000">//<span style="color: #008000">配置路由跳转到Users.vue这个组件
{path:"/Users"<span style="color: #000000">,component:Users}
],<span style="color: #008000">//<span style="color: #008000">去掉地址栏的"/#/"
mode : "history"<span style="color: #000000">
}
)
<span style="color: #008000">/<span style="color: #008000"> eslint-disable no-new <span style="color: #008000">/
<span style="color: #0000ff">new Vue({ <span style="color: #008000">//<span style="color: #008000">实例化一个vue对象
router,<span style="color: #008000">//<span style="color: #008000">使用路由
el: '#app',<span style="color: #008000">//<span style="color: #008000">index.html的根元素app
components: { App },<span style="color: #008000">//<span style="color: #008000">注册根组件App.vue才能使用
template: ''<span style="color: #008000">//<span style="color: #008000">VUE模板使用,可以是组件、html标签等
})
<span style="color: #008000">//
<span style="color: #008000">2. 行为:处理逻辑
<span style="color: #008000">//<span style="color: #008000">3. 样式:解决样式