vue项目中,基地址变化的配置

当需要请求的基地址变化的时候,可以将基地址配置成一个config.json文件放在服务器上,在main.js中读取配置的地址,从而改变请求的基地址.

1.在main.js中获取跳转的地址

axios
  .get("config.json")
  .then(res => {
    Vue.prototype.platformUrl = res.data.VUE_APP_PLATFORM_API_PATHONE;
    sessionStorage.setItem(
      "platformUrl",
      JSON.stringify(Vue.prototype.platformUrl)
    );
    Vue.prototype.casUrl = res.data.VUE_APP_CAS_PATH;
    Vue.prototype.jwtUrl = res.data.VUE_APP_JWT_PATH;
    Vue.prototype.platformGatewayUrl = res.data.VUE_APP_PLATFORM_API_PATH;

    if (jwt == null) {
      // getToken(Vue.prototype.jwtUrl, Vue.prototype.casUrl);
      // const code = window.location.href.split('=')[1];
      const params = new URLSearchParams(window.location.search);
      let code = params.get('code')
      if (code == null) {
        getToken(Vue.prototype.jwtUrl, Vue.prototype.casUrl)
      } else {
        const jtcode = code.split("#/")[0]
        tjLogin(jtcode, res.data.VUE_APP_PLATFORM_API_PATH, res.data.CLIENT_ID, res.data.CLIENT_SECRET, res.data.redirectUri)
      }
    } else {
      if (sessionStorage.getItem("isuserConfig")) {
        findMenu(JSON.parse(sessionStorage.getItem("isuserConfig")).userCode);
      }
      if (sessionStorage.getItem("userConfigInfo")) {
        findMenu(JSON.parse(sessionStorage.getItem("userConfigInfo")).userCode);
      }
      if (sessionStorage.getItem("userConfig")) {
        findMenu(JSON.parse(sessionStorage.getItem("userConfig")).userCode);
      }
    }
  })
  .catch(err => {
    console.log(err);
  });

2.配置require.js的基地址可变

 

import axios from "axios";
export function request(config) {
  const instance = axios.create({
    baseURL: JSON.parse(sessionStorage.getItem("platformUrl")),
    // baseURL: "/api",
    timeout: 5000
  });


  instance.interceptors.request.use(
    config => {
      const jwt = sessionStorage.getItem("jwt");
      if (jwt != null) {
        config.headers.Authorization = jwt;
      }
      return config;
    },
    err => {
      console.log("请求拦截err: " + err);
    }
  );

  instance.interceptors.response.use(
    res => {
      return res.data;
    },
    err => {
      console.log("响应拦截err: " + err);
    }
  );

  return instance(config);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 项目实现多语言配置的方法很多,其一种常见的做法是使用 `vue-i18n` 库。下面是使用 `vue-i18n` 实现多语言配置的步骤: 1. 安装 `vue-i18n` 库。 ```shell npm install vue-i18n --save ``` 2. 在 `main.js` 引入 `vue-i18n` 库,并初始化配置。 ```javascript import Vue from 'vue' import VueI18n from 'vue-i18n' import App from './App.vue' Vue.use(VueI18n) const i18n = new VueI18n({ locale: 'en', // 设置默认语言为英语 messages: { en: { greeting: 'Hello!' }, zh: { greeting: '你好!' } } }) new Vue({ el: '#app', i18n, render: h => h(App) }) ``` 在上面的代码,首先引入 `vue-i18n` 库,并通过 `Vue.use()` 方法将其安装到 Vue 。然后,初始化 `VueI18n` 实例,并设置默认语言为英语,同时设置了英语和文两种语言的翻译内容。 3. 在组件使用 `$t` 方法来获取翻译内容。 ```html <template> <div> <p>{{ $t('greeting') }}</p> </div> </template> ``` 在上面的代码,我们使用 `$t` 方法来获取 `greeting` 这个翻译内容。`$t` 方法会根据当前语言环境自动获取对应的翻译内容,如果找不到对应的翻译,则会返回原始字符串。 4. 在页面添加语言切换按钮,以便用户可以切换不同的语言。 ```html <template> <div> <p>{{ $t('greeting') }}</p> <button @click="switchLang('en')">English</button> <button @click="switchLang('zh')">文</button> </div> </template> <script> export default { methods: { switchLang(lang) { this.$i18n.locale = lang // 切换语言 } } } </script> ``` 在上面的代码,我们添加了两个按钮,分别用于切换为英语和文。当用户点击按钮时,会触发 `switchLang` 方法来切换当前的语言环境。在 `switchLang` 方法,我们通过修改 `$i18n.locale` 属性来切换当前语言环境。 通过上面的步骤,我们就可以在 Vue 项目实现多语言配置了。当用户切换语言时,页面的文本内容会自动切换为对应的翻译内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值