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是頁面接收到的需要使用的數據
    })
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM