1、首先下載安裝axios
npm install --save axios
2、在項目main.js里 引入下面代碼
import axios from 'axios'
Vue.prototype.$http = axios
3、在需要用到axos的組件<script>標簽中引入如下代碼
import axios from 'axios'
4、組件methods中定義function
methods: { getdata: function() { var url = "https://cnodejs.org/api/v1/topics"; // 發送請求:將數據返回到一個回到函數中 var that = this; // 並且響應成功以后會執行then方法中的回調函數 axios.get(url).then(function(result) { // result是所有的返回回來的數據 // 包括了響應報文行 // 響應報文頭 // 響應報文體 console.log(result.data.data); that.goodlist = result.data.data; }); } }
5、在組件HTML中設置@click事件 getdata (點擊后請求數據)
6、將獲取的數據在頁面渲染