美文网首页
8.Axios使用

8.Axios使用

作者: nora_wang | 来源:发表于2019-06-23 22:36 被阅读0次

1.npm下载安装

$ npm install axios 

2.在main.js中引入该模块并挂载到vue原型上

import Axios from 'axios'
Vue.prototype.$axios = Axios

3.组件中使用axios
post:

<script>
    import qs from 'qs'
    export default {
        name: "posts",
        data(){
            return{
                postData:[]
            }
        },
        //post请求的参数必须为x-www-form-urlencoded,所以需要引入第三方qs库,通过qs.stringify的方式将form-data格式的值转为x-www-form-urlencoded,如果不转换,则将参数直接以form-data的格式拼接在接口连接后面。
        created(){
            this.$axios.post("http://www.wwtliu.com/sxtstu/blueberrypai/login.php",qs.stringify({
                user_id:""
            })).then(res=>{
                console.log(res);
            }).catch(error=>{
                console.log(error);
            })
        }
    }
</script>

get:(测试阶段需解决跨域问题)

<script>
    export default {
        name: "doubanAxios",
        data(){
            return{
                doubanData:[]
            }
        },
        created(){
            var url = this.HOST + '/v2/movie/coming?apikey=0df993c66c0c636e29ecbb5344252a4a';
            this.$axios({
                method:'get',
                url:url,
            }).then(res=>{
                console.log(res);
            }).catch(error=>{
                console.log(error);
            });
        }
    }
</script>

相关文章

网友评论

      本文标题:8.Axios使用

      本文链接:https://www.haomeiwen.com/subject/pmtgqctx.html