You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This wrapper bind `axios` to `app` instance or `this` if you're using single file component.
69
+
70
+
in option API, you can use `axios` like this:
71
+
72
+
```js
73
+
// App.vue
74
+
exportdefault {
75
+
name:'App',
76
+
methods: {
77
+
getList() {
78
+
this.axios.get(api).then((response) => {
79
+
console.log(response.data)
80
+
})
81
+
// or
82
+
this.$http.get(api).then((response) => {
83
+
console.log(response.data)
84
+
})
85
+
}
86
+
}
87
+
}
88
+
```
89
+
90
+
however, in composition API `setup` we can't use `this`, you should use `provide` API to share the globally instance properties first, then use `inject` API to inject `axios` to `setup`:
91
+
92
+
```js
93
+
// main.ts
94
+
import { createApp } from'vue'
95
+
importAppfrom'./App.vue'
96
+
importstorefrom'./store'
97
+
importaxiosfrom'axios'
98
+
importVueAxiosfrom'vue-axios'
99
+
100
+
constapp=createApp(App).use(store)
101
+
app.use(VueAxios, axios)
102
+
app.provide('axios', app.config.globalProperties.axios) // provide 'axios'
103
+
app.mount('#app')
104
+
105
+
// App.vue
106
+
import { inject } from'vue'
107
+
108
+
exportdefault {
109
+
name:'Comp',
110
+
setup() {
111
+
const axios:any=inject('axios') // inject axios
112
+
113
+
constgetList= ():void=> {
114
+
axios
115
+
.get(api)
116
+
.then((response: { data: any }) => {
117
+
console.log(response.data)
118
+
});
119
+
};
120
+
121
+
return { getList }
122
+
}
123
+
}
124
+
```
125
+
63
126
Please kindly check full documention of [axios](https://github.com/axios/axios) too
0 commit comments