一、安装
npm install @nuxtjs/axios @nuxtjs/proxy -s
二、配置
nuxt.config.js文件中增加配置
modules: [
'@nuxtjs/axios',//npm install @nuxtjs/axios -s
'@nuxtjs/proxy'
],
axios:{
proxy:true//是否可以跨域
},
proxy:{
'/api':{
target:'http://0.0.0.0:9870/',
pathRewrite:{
'^/api':'',
}
}
},
三、运用
index.vue
methods:{
login(){
let data = {
username : this.username,
password : this.password,
loginType : 2
}
this.$axios({
url:'/api/user/login',
method:'post',
data
}).then(res => {
this.setToken(res.data.accessToken)
this.$router.push({name:'index'})
})
}
}