Vue.js 2.x笔记:服务请求axios(8)

1. axios简介

  vue2.0之后,推荐使用axios。

  axios官方地址:https://github.com/axios/axios

2. axios安装

  npm安装:

npm install axios

  main.js引用:

import Vue from 'vue'
import axios from 'axios'

// 全局将axios改写成Vue的原型属性
Vue.prototype.$http = axios

3. axios使用示例

3.1 get

<template>
    <div class="home">
        <ul>
            <li v-for="todo in todos" :key="todo.id">
                {{ todo.ID }}-{{ todo.Name }}
            </li>
        </ul>
    </div>
</template>
<script>
import axios from 'axios';

export default {
    data() {
        return {
            todos: null
        };
    },
    created() {
        this.initToDos();
    },
    methods: {
        /**
         * 初始化TODO数据
         */
        initToDos() {
            axios
                .get('http://localhost/api/ToDo')
                .then(response => (this.todos = response.data.ToDos))
                .catch(function(error) {
                    console.log(error);
                });
        }
    }
};
</script>

3.2 post

posted @ 2018-06-27 11:59  libingql  阅读(462)  评论(0编辑  收藏  举报