欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

nuxt+axios实现打包后动态修改请求地址的方法

程序员文章站 2022-06-25 12:56:10
需求:把请求地址等一些配置暴露出来以便打包后动态修改,而不需要重新打包编译。这样也是挺不错的,当一个项目需要部署到几个不同的服务器上时候,就不用说每次都要修改后再重新打包。功能不变时,单单是修改一下请...

需求:把请求地址等一些配置暴露出来以便打包后动态修改,而不需要重新打包编译。

这样也是挺不错的,当一个项目需要部署到几个不同的服务器上时候,就不用说每次都要修改后再重新打包。功能不变时,单单是修改一下请求地址省了不少功夫。

1.开始

把需要动态修改的配置写在一个配置json文件里面。该配置文件可以放在static目录下:

静态文件目录:静态文件目录 static 用于存放应用的静态文件,此类文件不会被 nuxt.js 调用 webpack 进行构建编译处理。 服务器启动的时候,该目录下的文件会映射至应用的根路径 / 下。

2.实现

在static下新建baseconfig.json文件,把需要暴露出来的配置写上,先上代码:

{
 "video_dir": "video/",
 "base_host": "http://xxxxx"
 "request_prefix":'/api/'
}

有需求的可以扩展配置文件(想怎么写就怎么写,开心就行~),例如可以根据不用的环境(开发、生产)切换等;

在plugis文件夹下新建baseconfig.js文件:

import vue from 'vue';
import axios from 'axios';

const protocoltmp = window.location.protocol; // 获取当前 url 的协议
const { host } = window.location; // 获取当前host

<!--请求配置信息-->
async function getserverconfig() {
 return new promise(async (resolve, reject) => {
  await axios.get(`${protocoltmp}//${host}/baseconfig.json`).then((result) => {
   const { base_host,video_dir ,request_prefix} = result.data;
   //把配置挂载在vue属性上,以便调用
   vue.prototype.$base_host = base_host;
   vue.prototype.$request_prefix = request_prefix;
   vue.prototype.$video_dir = video_dir;
   resolve();
  }).catch((error) => {
   console.log('error', error);
   reject();
  });
 });
}
getserverconfig();

在项目配置文件nuxt.config.js中引入:

plugins: [
  ...
  '~/plugins/pathconfig'
 ],

最后在axios里面配置使用,完事。axios.js :

import qs from "qs"
import vue from 'vue';

export default function (e) {
 // e.$axios.defaults.baseurl = 'http://xxxxxxx/api/'
 // e.$axios.defaults.headers.post['content-type'] = 'application/x-www-form-urlencoded';
   e.$axios.defaults.baseurl = vue.prototype.$base_host + vue.prototype.$request_prefix
 
 // request interceptor
 e.$axios.interceptors.request.use(
  config => {
   // do something before request is sent
   if (config.method == 'post') {
    config.data = qs.stringify(config.data)
   }
   return config
  },
  error => {
   // do something with request error
   return promise.reject(error)
  }
 )
 // response interceptor
 e.$axios.interceptors.response.use(
  /**
   * determine the request status by custom code
   * here is just an example
   * you can also judge the status by http status code
   */
  response => {
   const res = response.data
   if (response.status === 200 && res.code == 1000) {
    return res
   } else {
    // redirect('/404')
    // if the custom code is not 200, it is judged as an error.
   }
   return promise.reject({ code: res.code, msg: res.msg || 'error' })
  },
  error => {
   console.log('err' + error) // for debug

   return promise.reject(error)
  }
 )

 e.$axios.onerror(error => {
  const code = parseint(error.response && error.response.status)
  if (code === 400) {
   // redirect('/400')
   console.log('$axios.onerror :', error)
  }
 })
}

3. 最后

到此这篇关于nuxt+axios实现打包后动态修改请求地址的方法的文章就介绍到这了,更多相关nuxt+axios 打包后动态修改请求地址内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!