Vue如何获取数据列表展示

所属分类: 网络编程 / JavaScript 阅读数: 1351
收藏 0 赞 0 分享

这个例子从 Github 的 API 中获取了最新的 Vue.js 提交数据,并且以列表形式将它们展示了出来。你可以轻松地切换 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.zhanzhang360.cn/imgupload/000038/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>当前选定:{{ 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>创建人:<a :href="item.author.html_url" > {{ item.commit.author.name }}</a></span><br/>
    <span>创建时间:{{ 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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

javascript if条件判断方法小结

今天在为网站增加一些代码功能的时候,需要用到if条件判断,发现简写方法忘了,这里特整理下
收藏 0 赞 0 分享

javascript教程:关于if简写语句优化的方法

这篇文章主要介绍了js中if简写语句优化的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

wap浏览自动跳转到wap页面的js代码

这篇文章主要介绍了如何让用户输入wap手机网站的网址时自动跳转到wap网站,需要的朋友可以参考下
收藏 0 赞 0 分享

jqGrid读取选择的多行的某个属性代码

这篇文章主要介绍了jqGrid读取选择的多行的某个属性实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

让alert不出现弹窗的两种方法

这篇文章主要介绍了让alert不出现弹窗的两种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

JSON+HTML实现国家省市联动选择效果

实现国家省市联动的方法有很多,本文要为大家介绍的JSON+HTML如何实现,需要的朋友可以参考下
收藏 0 赞 0 分享

javascript的alert box在java中如何显示多行

这篇文章主要介绍了javascript的alert box在java中如何显示多行,需要的朋友可以参考下
收藏 0 赞 0 分享

绑定回车enter事件代码

这篇文章主要介绍了绑定回车enter事件代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Jquery 返回json数据在IE浏览器中提示下载的问题

Jquery 返回json数据,IE浏览器提示下载的问题,当提交完数据后返回的本来是json数据的,在火弧里测试正常,解决方法如下
收藏 0 赞 0 分享

用jquery实现的一个超级简单的下拉菜单

这篇文章主要介绍了用jquery实现的一个超级简单的下拉菜单,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多