vue-cli webpack 開(kāi)發(fā)環(huán)境跨域詳解
edit dev.proxyTable option in config/index.js. The dev server is using http-proxy-middleware for proxying
為了解決跨域問(wèn)題,
- 通常會(huì)使用Jsonp,但是jsonp只能是get請(qǐng)求。
- 或者使用CORS支持,設(shè)置Access-Control-Allow-Origin: *
0 前置技能
熟悉vue-loader 和 webpack
1 基本配置
編輯confix/index.js文件 dev server使用的是http-proxy-middleware來(lái)代理
// config/index.js module.exports = { // ... dev: { proxyTable: { // proxy all requests starting with /api to jsonplaceholder '/api': { target: 'http://jsonplaceholder.typicode.com', changeOrigin: true, pathRewrite: { '^/api': '' } } } } }
The above example will proxy the request /api/posts/1 to http://jsonplaceholder.typicode.com/posts/1.
2 全局匹配
you can provide a filter option that can be a custom function to determine whether a request should be proxied:
提供一個(gè)過(guò)濾器,制定路由規(guī)則和路由方法。
proxyTable: { '*': { target: 'http://jsonplaceholder.typicode.com', filter: function (pathname, req) { return pathname.match('^/api') && req.method === 'GET' } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解vue-cli4 配置不同開(kāi)發(fā)環(huán)境打包命令
- vue-cli開(kāi)發(fā)環(huán)境實(shí)現(xiàn)跨域請(qǐng)求的方法
- 新版vue-cli模板下本地開(kāi)發(fā)環(huán)境使用node服務(wù)器跨域的方法
- 使用vue-cli+webpack搭建vue開(kāi)發(fā)環(huán)境的方法
- 詳解vue-cli開(kāi)發(fā)環(huán)境跨域問(wèn)題解決方案
- Vue-cli proxyTable 解決開(kāi)發(fā)環(huán)境的跨域問(wèn)題詳解
- vue cli 3.0下配置開(kāi)發(fā)環(huán)境下的sourcemap問(wèn)題
相關(guān)文章

vue中的路由跳轉(zhuǎn)tabBar圖片和文字的高亮效果

vue中動(dòng)態(tài)渲染數(shù)據(jù)時(shí)使用$refs無(wú)效的解決

使用vue實(shí)現(xiàn)點(diǎn)擊按鈕滑出面板的實(shí)現(xiàn)代碼