vue中使用axios

 

一、在项目中引进axios(npm引入)

npm install axios --save

 

二、main.js中引入axios

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

 

三、在需要使用的页面中使用

created(){
    this.$axios({ //请求数据(原生用法,未使用封装)
        url: "https://www.xinhuang.net.cn/wap/demo_mp", //请求地址
        method: "get" //请求方法
        // params: {}
    }).then(res => {
        console.log(res)
        console.log(res.data) //res.data是页面接收到的需要使用的数据
    })
}

 

四、可以在data里面建一个空变量,然后在获取到值的时候将之赋给空变量,之后就可以直接使用这个空变量,例如:

data(){
    return{
        demo:''
    }
},
created(){
    this.$axios({ //请求数据(原生用法,未使用封装)
        url: "https://www.xinhuang.net.cn/wap/demo_mp", //请求地址
        method: "get" //请求方法
        // params: {}
    }).then(res => {
        console.log(res)
        this.demo=res.data; //res.data是页面接收到的需要使用的数据
    })
}

 

posted @ 2020-08-15 14:37  远方的异特  阅读(1846)  评论(0编辑  收藏  举报