把nuxtjs-axios模块添加到项目
yarn add @nuxtjs/axios // 使用yarn来安装模块
npm install @nuxtjs/axios // 使用npm来安装模块
注:不同的包管理器,要使用不同的命令
配置nuxt.config.js
简单使用
modules: [
'@nuxtjs/axios'
],
如果这步完成,可以在项目中方法使用console.log(this) ,这时候可以看到该对象中有$axios属性了
如果你想使用代理可以这样写
axios: {
proxy: true,
// prefix: '/api/',
credentials: true
// See https://github.com/nuxt-community/axios-module#options
},
proxy: {
'/api/': {
target: 'https://h5api.zhefengle.cn', //这个网站是开源的可以请求到数据的
pathRewrite: {
'^/api/': '/',
changeOrigin: true
}
}
},
使用
let that=this;
this.$axios.get("/api/index.html").then(res=>{
alert(res.data.code);
});