溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用vue框架Ajax獲取數(shù)據(jù)列表并用BootStrap顯示出來(lái)

發(fā)布時(shí)間:2021-04-26 12:56:32 來(lái)源:億速云 閱讀:270 作者:小新 欄目:web開發(fā)

這篇文章主要介紹如何使用vue框架Ajax獲取數(shù)據(jù)列表并用BootStrap顯示出來(lái),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

vue是什么

Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫(kù)只關(guān)注視圖層,方便與第三方庫(kù)和項(xiàng)目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫(kù)開發(fā)復(fù)雜的單頁(yè)應(yīng)用。

1.第一步肯定是包的導(dǎo)入了

目前對(duì)node.js還不是很了解,所以包都是手動(dòng)下載導(dǎo)入的,沒(méi)有用到那個(gè)nmp

vue.min.js是vue的主包,vue-resource.js是開源的vue Ajax包,對(duì)ajax進(jìn)行了封裝

bootstrap既就是樣式的相關(guān)css和js了

<script type="text/javascript" src="/js/vue-resource.js"/>
<script type="text/javascript" src="/js/vue.min.js"/> 
<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<script dsrc="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>

-2.vue發(fā)送ajax請(qǐng)求

后臺(tái)數(shù)據(jù)已寫好,返回為json數(shù)據(jù)如下:

{
  "id": "1305120309",
  "violates": 0,
  "borrows": 0,
  "overdraft": 0,
  "notReturns": 0,
  "libraryBooks": [
    {
      "chargePerson": "王大海",
      "borrowTime": "2017-04-22 18:05:10",
      "name": "計(jì)算機(jī)入門",
      "bookId": "051301"
    },
    {
      "chargePerson": "王大海",
      "borrowTime": "2017-04-22 18:06:11",
      "name": "軟件工程",
      "bookId": "051302"
    }
  ]
}

vue Ajax請(qǐng)求代碼如下:

<script>
  Vue.use(VueResource);   //這個(gè)一定要加上,指的是調(diào)用vue-resource.js
  new Vue({
    el: '#vue-menu3',   //div的id
    data: {
      libraryInfo: ""  //數(shù)據(jù),名稱自定
    },
    created: function () { //created方法,頁(yè)面初始調(diào)用  
      var url = "/library/libraryInfo";
      this.$http.get(url).then(function (data) {  //ajax請(qǐng)求封裝
        var json = data.bodyText;
        var usedData= JSON.parse(json);
        //我的json數(shù)據(jù)參考下面
        this.libraryInfo = usedData["libraryBooks"];
      }, function (response) {   //返回失敗方法調(diào)用,暫不處理
        console.info(response);
      })
    }
  });
</script>

-3.界面列表顯示

table的class使用bootstrap樣式,其他樣式可見菜鳥教程

table標(biāo)簽tr開始遍歷libraryInfo數(shù)據(jù),語(yǔ)法為value in libraryInfo簡(jiǎn)單易懂

<div id="vue-menu3">
  <table class="table table-striped">
    <caption>借閱書籍列表</caption>
    <thead>
      <tr>
        <th>書籍編號(hào)</th>
        <th>書名</th>
        <th>管理人員</th>
        <th>借閱時(shí)期</th>
        <th>歸還時(shí)間</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="value in libraryInfo">
        <td>{{value.bookId}}</td>
        <td>{{value.name}}</td>
        <td>{{value.chargePerson}}</td>
        <td>{{value.borrowTime}}</td>
        <td>{{value.returnTime}}</td>
      </tr>
    </tbody>
   </table>
</div>

最后結(jié)果如下:

如何使用vue框架Ajax獲取數(shù)據(jù)列表并用BootStrap顯示出來(lái)

以上是“如何使用vue框架Ajax獲取數(shù)據(jù)列表并用BootStrap顯示出來(lái)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI