axios(不是基于Vue的,基于Promise)
1》下载:npm install axios --save
2》引入axios
import axios from 'axios'
Vue.prototype.axios = axios
3》使用
this.axios.get("路径").then()
DEMO:
main.js:(为了方便就在main.js中写了,实际上哪个需要就去相应的js中写)
import Vue from 'vue'
import App from './App.vue'
import router from './router'
Vue.config.productionTip = false
import axios from 'axios'
Vue.prototype.axios = axios
new Vue({
router,
render: h => h(App)
}).$mount('#app')
App.Vue:
<template>
<div id="app">
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view/>
{{http}}
</div>
</template>
<script>
export default{
computed:{
http(){
this.axios.get('http://localhost:8080/data.json',{
params:{
id:1
}
})
.then((res)=>{
console.log(res.data)
})
this.axios.post('http://localhost:8080/data.json',{
id:1
})
.then((res)=>{
console.log(res.data)
})
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
#nav {
padding: 30px;
}
#nav a {
font-weight: bold;
color: #2c3e50;
}
#nav a.router-link-exact-active {
color: #42b983;
}
</style>