最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue prototype Axios - Stack Overflow

programmeradmin1浏览0评论

I am new to vue and Quasar.

Now, I got how Vue works vaguely,

I was trying to prehend the boilerplate code which we get when we init Quasar

While initiating, I asked it to integrate axios and veux from cli

Now I was trying to prehend the boiler plate which is when stumbled upon axios.js file inside plugin folder

The file contain the following code

import axios from 'axios'

export default ({ Vue }) => {
  Vue.prototype.$axios = axios
}
  1. Can someone tell me what does this code do? Based on my understanding, it seems like it adds a method to vue known as axios so that we can use it globally?

  2. What could be the reason for using $axios? i.e Vue.prototype.$axios = axios, Can't we just do Vue.prototype.axios = axios? since it is eventually creating a property?

  3. If we can use axios globally (without importing it or in other words writing import axios from "axios"). Then how can we do it?

  4. I am guessing this will only work on .vue file?

  5. I am used to creating a helper function where I do all the network request, usually the helper function file would be networkRequest.js where I would import axios and make requests. That networkRequest.js is the single point from where all the requests are made. Since Vue.prototype.$axios = axios would only work on .vue file? Does it still make sense to use axios plugin which es in the boiler plate

I am new to vue and Quasar.

Now, I got how Vue works vaguely,

I was trying to prehend the boilerplate code which we get when we init Quasar

While initiating, I asked it to integrate axios and veux from cli

Now I was trying to prehend the boiler plate which is when stumbled upon axios.js file inside plugin folder

The file contain the following code

import axios from 'axios'

export default ({ Vue }) => {
  Vue.prototype.$axios = axios
}
  1. Can someone tell me what does this code do? Based on my understanding, it seems like it adds a method to vue known as axios so that we can use it globally?

  2. What could be the reason for using $axios? i.e Vue.prototype.$axios = axios, Can't we just do Vue.prototype.axios = axios? since it is eventually creating a property?

  3. If we can use axios globally (without importing it or in other words writing import axios from "axios"). Then how can we do it?

  4. I am guessing this will only work on .vue file?

  5. I am used to creating a helper function where I do all the network request, usually the helper function file would be networkRequest.js where I would import axios and make requests. That networkRequest.js is the single point from where all the requests are made. Since Vue.prototype.$axios = axios would only work on .vue file? Does it still make sense to use axios plugin which es in the boiler plate

Share Improve this question asked Feb 11, 2019 at 19:12 AlwaysblueAlwaysblue 12k44 gold badges141 silver badges252 bronze badges 3
  • 1 I suspect most of your answers will be answered here: Adding Instance Properties – Mark Commented Feb 11, 2019 at 19:47
  • 1 this solved my concerns. Thanks :) – Alwaysblue Commented Feb 11, 2019 at 19:54
  • For me app.prototype was unaccessible in a boot file, so to add my method in the boot file I had to do the following: app.config.globalProperties.$newFunction = () => {}, if you want to get access to a quasar function inside your boot file you can use the following: app.config.globalProperties.$q.notify(...) – redigaffi Commented Sep 17, 2021 at 16:10
Add a ment  | 

1 Answer 1

Reset to default 7
  1. Yes, you are correct. It creates a global instance of axios that is available for all the ponents of Vue. So, instead of importing axios in multiple files and creating multiple instance of it, you can create ONE instance and put all the mon properties together for that instance. For example, you can define interceptors and urls in one place rather than having them spread out all around.

  2. Vue defines it very well in their website

No magic is happening here. $ is a convention Vue uses for properties that are available to all instances. This avoids conflicts with any defined data, puted properties, or methods.

  1. You can do it in multiple ways. If you are in a .vue file, you can directly access it through this.$axios(). If you want to access it through Vuex stores, you either need to pass the context of the ponent or you can use it in JS files import Vue from 'vue' and use it like Vue.prototype.$axios()

  2. It will also work in JS files. Follow the step in number 3.

  3. Refer to number 4.

发布评论

评论列表(0)

  1. 暂无评论