zoukankan      html  css  js  c++  java
  • vue——统一配置axios的baseUrl和所有请求的路径

    1. 配置baseUrl

    在package.json同级目录下新建一个faceConfig.js文件

     faceConfig.js:

    // 前端所有配置放这里
    const faceConfig = () => {
      return {
        // 正式环节
        'devServer': window.location.origin + '***' //***为上下文路径
      }
    }
    module.exports = faceConfig()

    2. 在src下新建api文件夹,并在其下面新建index.js文件

     index.js:

    const API_ROOT = require('../../faceConfig.js').devServer;
    export default { 
      /**  账号密码登录登出  **/
      loginIn: `${API_ROOT}/xxx`,
      logOut: `${API_ROOT}/xxx`,
      /** 数据库  **/
      list: `${API_ROOT}/xxx`, //列表
    }

    3. 组件中使用

    <template>
      <div>
            <button @click="exit">退出</button >
      </div>
    </template>
    
    <script>
      import API from '../api/index.js' //注意路径是否正确
    
      export default {
        name: 'Home',
        data() {
          return {}
        },
        methods: {
          exit() {
            let _this = this,
                token = localStorage.getItem('token');
            _this.$get(`${API.logout}?token=${token}`)  //` `是模板字符串,ES2015新增的符号
              .then((res) => {
                if (res.status == 200 && res.data.code == 1) {
                 ···
                } else {
                  ···
                }
              })
          }
        }
      }
    </script>
            
  • 相关阅读:
    VirtualApp
    python安装包遇到问题解决
    NMS_非极大值抑制的作用
    解释残差结构的有效性
    使用tcpdump命令抓取sql
    linux进程绑定cpu内核
    查询表空间占用情况
    数据库表分区
    Windows下如何使用ab命令做并发测试
    TCP协议
  • 原文地址:https://www.cnblogs.com/linjiangxian/p/13131277.html
Copyright © 2011-2022 走看看