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