Vue如何獲取數(shù)據(jù)列表展示
這個(gè)例子從 Github 的 API 中獲取了最新的 Vue.js 提交數(shù)據(jù),并且以列表形式將它們展示了出來(lái)。你可以輕松地切換 master 和 dev 分支。
一、展示

二、源碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.7/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h2>title</h2>
<template v-for="(branch, index) in branches">
<input type="radio"
:id=index
:value="branch"
v-model="currentBranch"
/>
<label :for="index">{{ branch }}</label>
</template>
<div>當(dāng)前選定:{{ currentBranch }}</div>
<ul>
<li v-for="item in getData">
<a :href="item.html_url" rel="external nofollow" >{{ item.sha.slice(0, 7) }}</a>
- <span>{{ item.commit.message }}</span><br/>
<span>創(chuàng)建人:<a :href="item.author.html_url" > {{ item.commit.author.name }}</a></span><br/>
<span>創(chuàng)建時(shí)間:{{ item.commit.author.date | formatDate }}</span>
</li>
</ul>
</div>
<script>
let apiURL = 'https://api.github.com/repos/vuejs/vue/commits?per_page=3&sha='
let vm = new Vue({
el: '#app',
data() {
return ({
branches: ['master', 'dev'],
currentBranch: 'master',
getData: null,
});
},
created() {
this.fetchDate();
},
watch: {
currentBranch: 'fetchDate'
},
filters: {
formatDate(v) {
return v.replace(/T|Z/g, ' ');
}
},
methods: {
fetchDate() {
var xhr;
if(window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
let self = this;
xhr.onload = function() {
if(xhr.readyState == 4) {
if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
self.getData = JSON.parse(xhr.responseText);
}else {
console.error(xhr.status, xhr.statusText);
}
}
}
xhr.open('GET', apiURL + this.currentBranch);
xhr.send(null);
}
},
});
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue+elementUI實(shí)現(xiàn)動(dòng)態(tài)展示列表的數(shù)據(jù)
- vue?使用el-table循環(huán)輪播數(shù)據(jù)列表的實(shí)現(xiàn)
- vue實(shí)現(xiàn)動(dòng)態(tài)列表尾部添加數(shù)據(jù)執(zhí)行動(dòng)畫(huà)
- vue如何從后臺(tái)獲取數(shù)據(jù)生成動(dòng)態(tài)菜單列表
- Vue3 列表界面數(shù)據(jù)展示詳情
- 使用Vue3進(jìn)行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)
- vue列表數(shù)據(jù)刪除后主動(dòng)刷新頁(yè)面及刷新方法詳解
- vue2.0實(shí)現(xiàn)列表數(shù)據(jù)增加和刪除
- vue input輸入框關(guān)鍵字篩選檢索列表數(shù)據(jù)展示
- vue父列表數(shù)據(jù)獲取子列表數(shù)據(jù)的實(shí)現(xiàn)步驟
相關(guān)文章
vue+elementUI動(dòng)態(tài)增加表單項(xiàng)并添加驗(yàn)證的代碼詳解
這篇文章主要介紹了vue+elementUI動(dòng)態(tài)增加表單項(xiàng)并添加驗(yàn)證的代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
在vue中實(shí)現(xiàn)點(diǎn)擊選擇框阻止彈出層消失的方法
今天小編就為大家分享一篇在vue中實(shí)現(xiàn)點(diǎn)擊選擇框阻止彈出層消失的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue限制輸入框只能輸入8位整數(shù)和2位小數(shù)的代碼
這篇文章主要介紹了vue限制輸入框只能輸入8位整數(shù)和2位小數(shù),文中我們使用v-model加watch 實(shí)現(xiàn)這一個(gè)功能,代碼簡(jiǎn)單易懂,需要的朋友可以參考下2019-11-11
公共Hooks封裝useTableData表格數(shù)據(jù)實(shí)例
這篇文章主要為大家介紹了公共Hooks封裝useTableData表格數(shù)據(jù)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Vue中mixins的使用方法以及實(shí)際項(xiàng)目應(yīng)用指南
vue中提供了一種混合機(jī)制--mixins,用來(lái)更高效的實(shí)現(xiàn)組件內(nèi)容的復(fù)用,下面這篇文章主要給大家介紹了關(guān)于Vue中mixins的使用方法以及實(shí)際項(xiàng)目應(yīng)用指南,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
Vue實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建和刪除數(shù)據(jù)的方法
下面小編就為大家分享一篇Vue實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建和刪除數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03

