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>
            
  • 相关阅读:
    SGU 495 Kids and Prizes
    HDU 3853 LOOPS
    HDU 4089 Activation
    HDU 4405 Aeroplane chess
    ZOJ 3329 One Person Game
    POJ 2096 Collecting Bugs
    POJ1573(Robot Motion)
    poj2632(Crashing Robots)
    poj1068(Parencodings)
    poj2506(Tiling)
  • 原文地址:https://www.cnblogs.com/linjiangxian/p/13131277.html
Copyright © 2011-2022 走看看