简体   繁体   English

axios拦截器处理程序如何在其创建方法正在调用axios.get的内部访问vue组件实例(此指针)?

[英]how can axios interceptor handler get access to vue component instance(this pointer)within whose created method is invoking axios.get?

Axios is a great library capable of doing ajax both in browser and in node environment. Axios是一个很棒的库,能够在浏览器和节点环境中执行ajax。 vuejs is a great framework used for component based web development. vuejs是用于基于组件的Web开发的出色框架。 Normally, it is excellent for a vue comonent to use axios to launch ajax operation. 通常,对于vue组件,使用axios启动ajax操作是非常好的。 According to https://github.com/imcvampire/vue-axios , we can use following code to integrate vue and axios. 根据https://github.com/imcvampire/vue-axios ,我们可以使用以下代码集成vue和axios。

import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
Vue.prototype.$http.interceptors.request.use(function(request){
    // I want to access the vuecomponent instance here, how can i do that??
    console.log((this.$options._componentTag ? (this.$options._componentTag+ 
   ' of '+ (this.$parent.$options._componentTag? 
   this.$parent.$options._componentTag:'root')): 'root component')+ ' for 
   url: ' 
   +request.url+ ' body: '+JSON.stringify(request.body))
}
   return request
},function(error){ return Promise.reject(error)});
// normally, following code will be run within vue component created hook 
   method, this represent the vuecomponent, $http is axio
this.$http.get(api).then((response) => {
   console.log(response.data)
})

and, also, i'd like to know in which vue component the ajax operation is being executed. 而且,我想知道在哪个vue组件中执行ajax操作。 So, i use the interceptors to address that problem. 因此,我使用拦截器来解决该问题。 Unfortunately, this pointer does not represent the vuecomponent, how can i implement that? 不幸的是,该指针不代表vue组件,我该如何实现呢?

I don't use vue-axios library, but you can implement it by using vue mixin like sample below: 我不使用vue-axios库,但是您可以通过使用vue mixin来实现它,例如以下示例:

let httpMixin = {
  methods: {
    get: function (url) {
      // You can access `this` here
      // ...

      return this.$http.get(url)
    }
  }
}

new Vue({
  mixins: [httpMixin],
  created: function () {
      // Use mixin methods instead $http
      this.get('/api/xyz').then(...)
  }
})

Just make it more reusable :') 只是使其更具可重用性:')

The package/plugin vue-axios is not my favorite. 软件包/插件vue-axios不是我的最爱。 If you google vue axios it's the first result. 如果您使用google vue axios这是第一个结果。 And I think that's the main reason of it's popularity. 我认为这是它受欢迎的主要原因。 But this is an alternative. 但这是另一种选择。 We just override the Vue.prototype.$http and Vue.prototype.axios using the original Axios Library 我们只是使用原始Axios库覆盖Vue.prototype.$httpVue.prototype.axios

You don't need to use vue-axios : 您不需要使用vue-axios

Vue.prototype.$http = axios;
Vue.prototype.axios = axios;

As we can see it takes the same amount of lines to write the whole functionality ourselves as it takes to configure the plugin. 如我们所见,自行编写整个功能所需的行数与配置插件所需的行数相同。

 const api = 'https://dog.ceo/api/breeds/list/all'; let childComp; Vue.prototype.$http = axios; Vue.prototype.axios = axios; childComp = { template: '<div><p>{{msg}}</p></div>', created() { console.log('child created'); this.$options._componentTag = 'tag-1'; this.$parent.$options._componentTag = 'parent-tag-1'; }, mounted() { console.log('child mounted'); }, data() { return { msg: 'Hello Vue', } }, } Vue.mixin({ created() { console.log('parent created'); } }) const app = new Vue({ el: '#app', render: h => h(childComp), methods: { load: function() { app.$http.interceptors.request.use(function(request) { //console.log(app.$options.components); // I want to access the vuecomponent instance here, how can i do that?? let name = app.$options._componentTag; console.log(name); let parentName = app.$root.$options._componentTag; console.log('C'); console.log( (name ? (name + ' of ' + (parentName ? parentName : 'root')) : 'root component') + ' for url: ' + request.url + ' body: ' + JSON.stringify(request.body)); return request; }, function(error) { return Promise.reject(error) }); // normally, following code will be run within vue component created hook // method, this represent the vuecomponent, $http is axios app.$http.get(api).then((response) => { //console.log(response.data); }); }, }, }); app.load(); var Ctor = Vue.extend({ name: 'cool-stuff' }); var vm = new Ctor(); // console.log(vm); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/vuex/3.1.1/vuex.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.js"></script> <div id="app"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM