经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JSON » 查看文章
vue如何加载本地json数据
来源:jb51  时间:2022/4/7 10:49:23  对本文有异议

vue加载本地json数据

json数据存放在除static静态文件夹中

这种方法暂时还没出来,若有大神知道,可否能指导一二

json数据存放在static静态文件夹中

1、编写好json 数据,按照这个格式编写json数据

2、安装axios 安装方法:npm install axios

3、配置axios,在main.js中引用axios,如下图所示

4、就可以调用json数据了,也可以加上一句:console.log(this.fieldParams)在控制台打印数据

表格代码:

  1. <el-table
  2. :data="fieldParams"
  3. border
  4. style="width:100%"
  5. >
  6. </el-table>

读取本地json文件并分页显示

功能实现

通过axios异步加载技术读取本地的json文件内容,并通过vue.js处理数据在h5页面分页显示(这里以3行数据分页)

student.json数据如下

  1. [
  2. {"stuId":1,"stuName":"李华","stuSex":"男","stuAge":20},
  3. {"stuId":2,"stuName":"张国伟","stuSex":"男","stuAge":22},
  4. {"stuId":3,"stuName":"刘艳","stuSex":"女","stuAge":19},
  5. {"stuId":4,"stuName":"李小燕","stuSex":"女","stuAge":22},
  6. {"stuId":5,"stuName":"张鹏","stuSex":"男","stuAge":26},
  7. {"stuId":6,"stuName":"李晔","stuSex":"女","stuAge":20},
  8. {"stuId":7,"stuName":"钱国强","stuSex":"男","stuAge":21},
  9. {"stuId":8,"stuName":"张三","stuSex":"男","stuAge":22},
  10. {"stuId":9,"stuName":"唐毓民","stuSex":"男","stuAge":25},
  11. {"stuId":10,"stuName":"玛丽亚","stuSex":"女","stuAge":21},
  12. {"stuId":11,"stuName":"李家明","stuSex":"男","stuAge":21}
  13. ]

h5代码如下

  1. <body>
  2. <div id ="app" v-cloak>
  3. <table border="1px" style="width: 400px;" class="table table-striped table-bordered table-hover table-condensed">
  4. <thead>
  5. <tr>
  6. <th>序号</th>
  7. <th>姓名</th>
  8. <th>性别</th>
  9. <th>年龄</th>
  10. </tr>
  11. </thead>
  12. <tr v-for="student in stuData">
  13. <td>{{ student.stuId }}</td>
  14. <td>{{ student.stuName }}</td>
  15. <td>{{ student.stuSex }}</td>
  16. <td>{{ student.stuAge }}</td>
  17. </tr>
  18. </table>
  19. <!-- 用无序列表做一个页码导航条-->
  20. <ul>
  21. <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="prePage"> < </a></li>
  22. <li v-for="(value,index) in pageNumber">
  23. <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="thisPage(index)">{{ index+1 }}</a>
  24. </li>
  25. <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="nextPage"> > </a></li>
  26. </ul>
  27. </div>
  28. </body>

css样式

  1. <style>
  2. [v-cloak]{
  3. display: none;
  4. }
  5. ul{
  6. margin-left: 20px;
  7. }
  8. ul li{
  9. float: left;
  10. list-style: none;
  11. background-color: aqua;
  12. }
  13. ul li a{
  14. text-decoration: none;
  15. padding: 5px 15px;
  16. color:black;
  17. border: 1px solid white;
  18. }
  19. a:hover{
  20. background: tomato;
  21. }
  22. </style>

js代码

  1. <script>
  2. //创建Vue实例,得到 ViewModel
  3. var app = new Vue({
  4. el: '#app',
  5. data: {
  6. list:[],
  7. pageSize:3,//每页大小
  8. currentPage:0 //当前页码
  9. },/*数据*/
  10. mounted(){
  11. //异步加载json数据
  12. axios.get('/json/student.json',{}).then(function(response){
  13. app.list=response.data;
  14. });
  15. },/*自动加载函数*/
  16. methods: {
  17. //上一页
  18. nextPage: function(){
  19. if (this.currentPage == this.pageNumber - 1) return;
  20. this.currentPage++;
  21. },
  22. //下一页
  23. prePage: function(){
  24. if (this.currentPage == 0) return;
  25. this.currentPage--;
  26. },
  27. //页码
  28. thisPage: function(index){
  29. this.currentPage = index;
  30. }
  31. },/*执行触发函数*/
  32. computed: {
  33. //分页数据
  34. stuData: function(){
  35. let left = this.currentPage*this.pageSize;
  36. let right = Math.min((this.currentPage+1)*this.pageSize, this.list.length)
  37. return this.list.slice(left, right);//取出一页数据
  38. },
  39. //共有多少页
  40. pageNumber: function(){
  41. if(this.list.length<=this.pageSize){
  42. return 1;
  43. }
  44. return Math.ceil(this.list.length / this.pageSize);
  45. }
  46. },/*动态计算属性*/
  47. });
  48. </script>

运行效果

在这里插入图片描述

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号