vue如何加載本地json數(shù)據(jù)
vue加載本地json數(shù)據(jù)
json數(shù)據(jù)存放在除static靜態(tài)文件夾中
這種方法暫時(shí)還沒出來,若有大神知道,可否能指導(dǎo)一二
json數(shù)據(jù)存放在static靜態(tài)文件夾中
1、編寫好json 數(shù)據(jù),按照這個(gè)格式編寫json數(shù)據(jù)

2、安裝axios 安裝方法:npm install axios
3、配置axios,在main.js中引用axios,如下圖所示

4、就可以調(diào)用json數(shù)據(jù)了,也可以加上一句:console.log(this.fieldParams)在控制臺(tái)打印數(shù)據(jù)

表格代碼:
<el-table
:data="fieldParams"
border
style="width:100%"
>
</el-table>讀取本地json文件并分頁顯示
功能實(shí)現(xiàn)
通過axios異步加載技術(shù)讀取本地的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":"錢國強(qiáng)","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>序號(hào)</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>
<!-- 用無序列表做一個(gè)頁碼導(dǎo)航條-->
<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實(shí)例,得到 ViewModel
var app = new Vue({
el: '#app',
data: {
list:[],
pageSize:3,//每頁大小
currentPage:0 //當(dāng)前頁碼
},/*數(shù)據(jù)*/
mounted(){
//異步加載json數(shù)據(jù)
axios.get('/json/student.json',{}).then(function(response){
app.list=response.data;
});
},/*自動(dòng)加載函數(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);
}
},/*動(dòng)態(tài)計(jì)算屬性*/
});
</script>
運(yùn)行效果

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用vue和datatables進(jìn)行表格的服務(wù)器端分頁實(shí)例代碼
本篇文章主要介紹了使用vue和datatables進(jìn)行表格的服務(wù)器端分頁實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
如何用vue3+Element?plus實(shí)現(xiàn)一個(gè)完整登錄功能
要實(shí)現(xiàn)用戶的登錄功能,可以使用Vue3和Element?Plus,下面這篇文章主要給大家介紹了關(guān)于如何基于Vue3和Element?Plus組件庫實(shí)現(xiàn)一個(gè)完整的登錄功能,文中提供了詳細(xì)的代碼示例,需要的朋友可以參考下2023-10-10
vue?實(shí)現(xiàn)左滑圖片驗(yàn)證功能
網(wǎng)頁中滑動(dòng)圖片驗(yàn)證一直是各大網(wǎng)站、移動(dòng)端的主流校驗(yàn)方式,其主要作用是為了區(qū)分人和機(jī)器以及為了防止機(jī)器人程序暴力登錄或攻擊從而設(shè)置的一種安全保護(hù)方式,這篇文章主要介紹了vue?實(shí)現(xiàn)左滑圖片驗(yàn)證,需要的朋友可以參考下2023-04-04
Vue實(shí)現(xiàn)一種簡(jiǎn)單的無限循環(huán)滾動(dòng)動(dòng)畫的示例
這篇文章主要介紹了Vue實(shí)現(xiàn)一種簡(jiǎn)單的無限循環(huán)滾動(dòng)動(dòng)畫的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Vue實(shí)現(xiàn)實(shí)時(shí)更新sessionStorage數(shù)據(jù)的示例代碼
這篇文章主要為大家詳細(xì)介紹了Vue如何實(shí)現(xiàn)實(shí)時(shí)更新sessionStorage數(shù)據(jù),文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以參考一下2023-06-06
在vue+element ui框架里實(shí)現(xiàn)lodash的debounce防抖
今天小編就為大家分享一篇在vue+element ui框架里實(shí)現(xiàn)lodash的debounce防抖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Vue使用js-audio-recorder實(shí)現(xiàn)錄制,播放與下載音頻功能
這篇文章主要為大家詳細(xì)介紹了Vue如何使用js-audio-recorder實(shí)現(xiàn)錄制,播放與下載音頻功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2023-12-12

