vue如何加載本地json數(shù)據(jù)
vue加載本地json數(shù)據(jù)
json數(shù)據(jù)存放在除static靜態(tài)文件夾中
這種方法暫時還沒出來,若有大神知道,可否能指導一二
json數(shù)據(jù)存放在static靜態(tài)文件夾中
1、編寫好json 數(shù)據(jù),按照這個格式編寫json數(shù)據(jù)
2、安裝axios 安裝方法:npm install axios
3、配置axios,在main.js中引用axios,如下圖所示
4、就可以調(diào)用json數(shù)據(jù)了,也可以加上一句:console.log(this.fieldParams)在控制臺打印數(shù)據(jù)
表格代碼:
<el-table :data="fieldParams" border style="width:100%" > </el-table>
讀取本地json文件并分頁顯示
功能實現(xiàn)
通過axios異步加載技術讀取本地的json文件內(nèi)容,并通過vue.js處理數(shù)據(jù)在h5頁面分頁顯示(這里以3行數(shù)據(jù)分頁)
student.json數(shù)據(jù)如下
[ {"stuId":1,"stuName":"李華","stuSex":"男","stuAge":20}, {"stuId":2,"stuName":"張國偉","stuSex":"男","stuAge":22}, {"stuId":3,"stuName":"劉艷","stuSex":"女","stuAge":19}, {"stuId":4,"stuName":"李小燕","stuSex":"女","stuAge":22}, {"stuId":5,"stuName":"張鵬","stuSex":"男","stuAge":26}, {"stuId":6,"stuName":"李曄","stuSex":"女","stuAge":20}, {"stuId":7,"stuName":"錢國強","stuSex":"男","stuAge":21}, {"stuId":8,"stuName":"張三","stuSex":"男","stuAge":22}, {"stuId":9,"stuName":"唐毓民","stuSex":"男","stuAge":25}, {"stuId":10,"stuName":"瑪麗亞","stuSex":"女","stuAge":21}, {"stuId":11,"stuName":"李家明","stuSex":"男","stuAge":21} ]
h5代碼如下
<body> <div id ="app" v-cloak> <table border="1px" style="width: 400px;" class="table table-striped table-bordered table-hover table-condensed"> <thead> <tr> <th>序號</th> <th>姓名</th> <th>性別</th> <th>年齡</th> </tr> </thead> <tr v-for="student in stuData"> <td>{{ student.stuId }}</td> <td>{{ student.stuName }}</td> <td>{{ student.stuSex }}</td> <td>{{ student.stuAge }}</td> </tr> </table> <!-- 用無序列表做一個頁碼導航條--> <ul> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="prePage"> < </a></li> <li v-for="(value,index) in pageNumber"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="thisPage(index)">{{ index+1 }}</a> </li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="nextPage"> > </a></li> </ul> </div> </body>
css樣式
<style> [v-cloak]{ display: none; } ul{ margin-left: 20px; } ul li{ float: left; list-style: none; background-color: aqua; } ul li a{ text-decoration: none; padding: 5px 15px; color:black; border: 1px solid white; } a:hover{ background: tomato; } </style>
js代碼
<script> //創(chuàng)建Vue實例,得到 ViewModel var app = new Vue({ el: '#app', data: { list:[], pageSize:3,//每頁大小 currentPage:0 //當前頁碼 },/*數(shù)據(jù)*/ mounted(){ //異步加載json數(shù)據(jù) axios.get('/json/student.json',{}).then(function(response){ app.list=response.data; }); },/*自動加載函數(shù)*/ methods: { //上一頁 nextPage: function(){ if (this.currentPage == this.pageNumber - 1) return; this.currentPage++; }, //下一頁 prePage: function(){ if (this.currentPage == 0) return; this.currentPage--; }, //頁碼 thisPage: function(index){ this.currentPage = index; } },/*執(zhí)行觸發(fā)函數(shù)*/ computed: { //分頁數(shù)據(jù) stuData: function(){ let left = this.currentPage*this.pageSize; let right = Math.min((this.currentPage+1)*this.pageSize, this.list.length) return this.list.slice(left, right);//取出一頁數(shù)據(jù) }, //共有多少頁 pageNumber: function(){ if(this.list.length<=this.pageSize){ return 1; } return Math.ceil(this.list.length / this.pageSize); } },/*動態(tài)計算屬性*/ }); </script>
運行效果
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用vue和datatables進行表格的服務器端分頁實例代碼
本篇文章主要介紹了使用vue和datatables進行表格的服務器端分頁實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06如何用vue3+Element?plus實現(xiàn)一個完整登錄功能
要實現(xiàn)用戶的登錄功能,可以使用Vue3和Element?Plus,下面這篇文章主要給大家介紹了關于如何基于Vue3和Element?Plus組件庫實現(xiàn)一個完整的登錄功能,文中提供了詳細的代碼示例,需要的朋友可以參考下2023-10-10Vue實現(xiàn)一種簡單的無限循環(huán)滾動動畫的示例
這篇文章主要介紹了Vue實現(xiàn)一種簡單的無限循環(huán)滾動動畫的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01Vue實現(xiàn)實時更新sessionStorage數(shù)據(jù)的示例代碼
這篇文章主要為大家詳細介紹了Vue如何實現(xiàn)實時更新sessionStorage數(shù)據(jù),文中的示例代碼講解詳細,具有一定的參考價值,需要的可以參考一下2023-06-06在vue+element ui框架里實現(xiàn)lodash的debounce防抖
今天小編就為大家分享一篇在vue+element ui框架里實現(xiàn)lodash的debounce防抖,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue使用js-audio-recorder實現(xiàn)錄制,播放與下載音頻功能
這篇文章主要為大家詳細介紹了Vue如何使用js-audio-recorder實現(xiàn)錄制,播放與下載音頻功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2023-12-12