admin管理员组文章数量:1313782
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
}
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?
What could be the reason for using
$axios
? i.eVue.prototype.$axios = axios
, Can't we just doVue.prototype.axios = axios
? since it is eventually creating a property?If we can use axios globally (without importing it or in other words writing
import axios from "axios"
). Then how can we do it?I am guessing this will only work on .vue file?
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. ThatnetworkRequest.js
is the single point from where all the requests are made. SinceVue.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
}
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?
What could be the reason for using
$axios
? i.eVue.prototype.$axios = axios
, Can't we just doVue.prototype.axios = axios
? since it is eventually creating a property?If we can use axios globally (without importing it or in other words writing
import axios from "axios"
). Then how can we do it?I am guessing this will only work on .vue file?
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. ThatnetworkRequest.js
is the single point from where all the requests are made. SinceVue.prototype.$axios = axios
would only work on .vue file? Does it still make sense to use axios plugin which es in the boiler plate
- 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
1 Answer
Reset to default 7Yes, 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.
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.
You can do it in multiple ways. If you are in a
.vue
file, you can directly access it throughthis.$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 filesimport Vue from 'vue'
and use it likeVue.prototype.$axios()
It will also work in JS files. Follow the step in number 3.
Refer to number 4.
本文标签: javascriptVue prototype AxiosStack Overflow
版权声明:本文标题:javascript - Vue prototype Axios - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741955536a2406967.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论