猿问

axios 中如何拿到 vue实例?

require.js中 在控制台输出 window 可以看到有 Vue实例 但输出 window.Vue 却显示 undifind
主要是想通过 vue实例 调用vux的loading插件 但是怎么都拿不到这个实例 求帮忙

import axios from 'axios'


import  { LoadingPlugin  } from 'vux'

import Vue from 'vue'

Vue.use(LoadingPlugin)

 function fetch(config) {

  return new Promise((resolve, reject) => {

    const instance = axios.create({

      transformRequest: [function (data) {

       return data

      }]

    }); 

    instance.interceptors.request.use(config => {

      config.headers['Content-Type']= 'application/x-www-form-urlencoded';

      return config

    }, error => {

      Promise.reject(error)

    })

    instance(config).then(res => {

      resolve(res);

      //失败后执行的函数

    }).catch(err => {

      console.log(err);

      reject(err);

    })

  });

}


 function myaxios(url,type,data,msgtype) {

      return fetch({

        url: url,

        method: type,

        data: data,

        headers:  {

        }

      })

  }

    export default myaxios

main.js 中

import  myaxios  from './utils/require'
 Vue.prototype.$axios = myaxios


暮色呼如
浏览 1034回答 2
2回答

慕村9548890

你不是这里引入Vue实例了吗:直接打印console.log(Vue)啊

HUWWW

拿不到的话,在require.js 里面再 var vue = new Vue(); 声明一个试试。
随时随地看视频慕课网APP
我要回答