vue的create方法中连续写多个this.$axios
时间: 2023-06-26 19:05:19 浏览: 57
在 Vue 的 create 方法中连续写多个 this.$axios 是完全可行的。一般来说,在 Vue 组件中使用 axios 时,我们会将其挂载到 Vue.prototype 上,这样在组件中就可以通过 this.$axios 来使用了。
以下是一个示例代码:
```javascript
import axios from 'axios'
Vue.prototype.$axios = axios.create({
baseURL: 'https://api.example.com'
})
export default {
name: 'MyComponent',
created() {
this.getSomeData()
this.postSomeData()
},
methods: {
getSomeData() {
this.$axios.get('/data').then(response => {
console.log(response.data)
})
},
postSomeData() {
this.$axios.post('/data', { name: 'John Doe' }).then(response => {
console.log(response.data)
})
}
}
}
```
在上面的代码中,我们在 Vue.prototype 上挂载了一个 axios 实例,并将其命名为 $axios。在组件中的 created 钩子函数中,我们连续调用了两个方法 this.getSomeData 和 this.postSomeData,这两个方法都使用了 this.$axios 来发送请求。
希望能对你有所帮助!