zoukankan      html  css  js  c++  java
  • (四)vue.js 外部配置文件(参考)

    前文我们已经安装了axios,现在我们来配置外部文件

    1.在vue项目中的static目录下新增文件config.json

    {
        "apiUrl": "http://127.0.0.1:30010/support/"
    }

    2.在main.js中引入axios,并定义一个全局函数来获取配置文件

    import axios from 'axios'

    Vue.prototype.$http = axios Vue.prototype.getConfig = function () { this.$http.get('./static/config.json').then(res => { Vue.prototype.apiUrl = res.data.apiUrl }).catch(err => { console.log(err) }) }

    3.在app.vue里面调用getConfig()获取ApiUrl,使用时直接使用 this.ApiUrl+'/api/‘ 进行调用

    //调用getConfig()获取ApiUrl
    mounted() {
      this.getConfig();
    }

    4.输入npm run build进行打包,查看dist文件夹下的config.json文件,实现外部配置

  • 相关阅读:
    我罗斯方块最终篇
    我罗斯汇报作业一
    11组-Alpha冲刺-2/6
    11组-Alpha冲刺-1/6
    结对编程作业
    11组 团队展示
    第一次个人编程作业
    第一次博客作业
    寒假作业3
    寒假作业2
  • 原文地址:https://www.cnblogs.com/chendongbky/p/9939958.html
Copyright © 2011-2022 走看看