您好,登錄后才能下訂單哦!
本文介紹了vue下跨域設置的相關介紹,分享給大家,具體如下:
1、在使用vue開發(fā)的時候經(jīng)常要涉及到跨域的問題,其實在vue cli中是有我們設置跨域請求的文件的。
2、當跨域無法請求的時候我們可以修改工程下config文件夾下的index.js中的dev:{}部分。
dev: { env: require('./dev.env'), port: 8080, autoOpenBrowser: false, assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/api': { target: 'http://api.douban.com/v2', changeOrigin: true, pathRewrite: { '^/api': '' } } }, // CSS Sourcemaps off by default because relative paths are "buggy" // with this option, according to the CSS-Loader README // (https://github.com/webpack/css-loader#sourcemaps) // In our experience, they generally work as expected, // just be aware of this issue when enabling this option. cssSourceMap: false }
將target設置為我們需要訪問的域名。
3、然后在main.js中設置全局屬性:
Vue.prototype.HOST = '/api'
4、至此,我們就可以在全局使用這個域名了,如下:
var url = this.HOST + '/movie/in_theaters' this.$http.get(url).then(res => { this.movieList = res.data.subjects; },res => { console.info('調用失敗'); });
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。