一、在項目中引進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是頁面接收到的需要使用的數據 }) }