本文介紹了vue下跨域設置的相關介紹,分享給大家,具體如下:
1、在使用vue開發的時候經常要涉及到跨域的問題,其實在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('調用失敗'); });以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答