一、下载安装插件
npm install axios --save
二、下载vue-axios
npm install --save axios vue-axios
三、在src/main.js中,导入axios
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
Vue.prototype.$axios = axios
四、在static文件夹下写静态文件 home.json
{
"fuid": 1,
"account":"12322343243",
"phone": "13200000000",
"displayName": "代收款",
"companyName": "代收款科技公司",
"accountType":[
{"id":1,"name":"经销商","value":1},
{"id":2,"name":"客服","value":2}
],
"remark": "帐号的备注信息"
}
五、在组件中请求数据 (页面请求)
import axios from 'axios'
//get请求
this.$axios.get("/static/home.json",{
params: {
id: 123
}
})
.then(res=>{
console.log(res.data);
}).catch(function(error){
console.log("error init."+error)
})
//post请求
this.$axios.post("/static/home.json",{
userId: "888"
},{
headers: {
token: "tom"
}
}).then(res => {
console.log(res.data);
}).catch(function(error){
})
六、在浏览器中查看运行结果。
npm run dev