vue在main.js中发请求
时间: 2023-12-05 07:03:44 浏览: 97
在Vue的main.js中发请求,需要使用Vue的内置HTTP库VueResource或者使用第三方库如axios。
使用VueResource:
1. 在main.js中引入VueResource
```
import VueResource from 'vue-resource'
Vue.use(VueResource)
```
2. 在Vue实例中使用VueResource
```
new Vue({
el: '#app',
components: { App },
template: '<App/>',
mounted() {
this.$http.get('https://jsonplaceholder.typicode.com/posts')
.then(response => {
console.log(response.body)
})
}
})
```
使用axios:
1. 在main.js中引入axios
```
import axios from 'axios'
Vue.prototype.$http = axios
```
2. 在Vue实例中使用axios
```
new Vue({
el: '#app',
components: { App },
template: '<App/>',
mounted() {
this.$http.get('https://jsonplaceholder.typicode.com/posts')
.then(response => {
console.log(response.data)
})
}
})
```
阅读全文
相关推荐















