使用npm安装
$ npm install axios
使用 bower安装
$ bower install axios
使用 cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
main.js:导入
import axios from ‘axios‘
Vue.prototype.$http = axios
Vue.prototype.$http.defaults.baseURL = ‘‘ // `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL
调用接口使用axios:
1: 接口数据读取
querygraphic () { let _this = this this.$http.get(‘https://localhost:44314/api/Values‘).then(res => { _this.list = res.data }) data () { return { list: [] } 调用赋值:v-for="queryg in list":key="queryg.id" {{ queryg.version }}
2:按id数据读取
getgrid () { this.$http.get(‘https://localhost:44314/api/Values/‘ + this.id).then(res => { this.gettext = res.data[0] console.log(this.gettext) }) data () { return { id: this.$route.params.id, // 读取路由传过来的id gettext: {} } }, 调用赋值: {{ gettext.text }}
3:put更新
this.$http({ url: ‘https://localhost:44314/api/Gj/‘ + this.id, method: ‘put‘, contentType: ‘application/json;charset=UTF-8‘, data: { ‘id‘: this.newinfo.id, ‘method‘: this.newinfo.method, ‘text‘: this.newinfo.text, ‘type‘: this.newinfo.type }, dataType: ‘json‘ }).then(res => { console.log(res) if (res.status === 204) { this.open1() console.log(‘成功‘) } else { this.open4() console.log(‘失败‘) } }).catch(console.error.bind(console)) // 异常