在config/index.js中添加如下代碼
proxyTable: {
????????'/api': {? ? ?// 匹配所以‘a(chǎn)pi’開頭的請求路徑
????????????????target: 'http://api.douban.com/v2',? ? //設(shè)置端口路由的公共部分
????????????????changeOrigin: true,? ?// //支持跨域
????????????????pathRewrite: {? // //重寫路徑:去掉路徑開頭的'/api'
????????????????????????????'^/api': ''
? ? ? ? ? ? ? ? ? ? ? ? ? ? // '^/api':'http://localhost:4000'//重寫路徑
? ? ? ? ? ? ? ? ? ? ? ? ? ? // //這里是用'/api'來代替 target中的地址回铛,例如要調(diào)用“http://localhost:8443/qml/?greate=100”那么直接寫成'/api/qml/?greate=100'
????????????????????}
????????????}
},
如果是https的接口副硅,那么需要再配置一項,是
secure:true? ? ?
然后在main.js中設(shè)置全局屬性:
Vue.prototype.HOST = '/api'