nuxt作為SEO友好框架腻贰,配置跨域和一般的vue_li大體相同,又有同點(diǎn)播演,下面是兩種配置代理的方法
一宾巍、nuxt自帶代理proxy
1、安裝資源axios
npm install @nuxtjs/axios --save-dev
2顶霞、配置nuxt.config.js文件
module.exports = {
modules: [ '@nuxtjs/axios','@nuxtjs/proxy','@nuxtjs/dotenv' ],
axios: {
proxy: true, // 表示開啟代理
prefix: '/', // 表示給請(qǐng)求url加個(gè)前綴 /api
credentials: true // 表示跨域請(qǐng)求時(shí)是否需要使用憑證
},
proxy: {
'/api': {
target: 'http://11.92.53.207:9000', // 目標(biāo)接口域名
// target: 'http://192.168.0.168:9001', // 本地
changeOrigin: true, // 表示是否跨域
pathRewrite: {
'^/api': '/', // 把 /api 替換成 /
}}},}
3选浑、使用
當(dāng)你使用接口的時(shí)候玄叠,在接口地址前加上/api
二、安裝proxyTable
modules: [
? ? ? '@gauseen/nuxt-proxy',
? ? ],
? ? proxyTable: {
? ? ? '/api':{
? ? ? ? target: 'http://39.91.216.241:9035',
? ? ? ? ws: false, // 是否支持websocket
? ? ? ? pathRewrite: { '^/api': '' }
? ? ? }
? ? },
使用方法一致