這個(gè)例子從 Github 的 API 中獲取了最新的 Vue.js 提交數(shù)據(jù),并且以列表形式將它們展示了出來。你可以輕松地切換 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">
<h3>title</h3>
<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>
本文標(biāo)題:Vue如何獲取數(shù)據(jù)列表展示-創(chuàng)新互聯(lián)
文章路徑:http://chinadenli.net/article0/dgheoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、關(guān)鍵詞優(yōu)化、網(wǎng)站策劃、企業(yè)建站、面包屑導(dǎo)航、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容