How does vue load local json data
This article mainly explains "how vue loads local json data". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how vue loads local json data.
Vue loads local json data json data is stored in a static folder except static
This method has not come out yet. If a great god knows, can you guide one or two?
Json data is stored in a static static folder
1. Write json data and write json data according to this format
2. Install axios installation method: npm install axios
3. Configure axios to reference axios in main.js, as shown in the following figure
4. You can call json data, or you can add: console.log (this.fieldParams) prints the data in the console
Form Code:
The realization of the function of reading local json file and paging display
The contents of the local json file are read through the axios asynchronous loading technology, and the data is paged on the h6 page through vue.js processing (here paged with 3 lines of data)
Student.json data are as follows: {"stuId": 1, "stuName": "Li Hua", "stuSex": "male", "stuAge": 20}, {"stuId": 2, "stuName": "Zhang Guowei", "stuSex": "male", "stuAge": 22}, {"stuId": 3, "stuName": "Liu Yan", "stuSex": "female", "stuAge": 19}, {"stuId": 4, "stuName": "Li Xiaoyan", "stuSex": "female" "stuAge": 22}, {"stuId": 5, "stuName": "Zhang Peng", "stuSex": "male", "stuAge": 26}, {"stuId": 6, "stuName": "Li Ye", "stuSex": "female", "stuAge": 20}, {"stuId": 7, "stuName": "Qian Guoqiang", "stuSex": "male", "stuAge": 21}, {"stuId": 8, "stuName": "Zhang San", "stuSex": "male" "stuAge": 22}, {"stuId": 9, "stuName": "Tang Yumin", "stuSex": "male", "stuAge": 25}, {"stuId": 10, "stuName": "Maria", "stuSex": "female", "stuAge": 21}, {"stuId": 11, "stuName": "Li Jiaming", "stuSex": "male" "stuAge": 21}] H6 code is as follows: serial number, name, sex, age {{student.stuId}} {{student.stuName}} {{student.stuSex}} {{student.stuAge}}
< {{ index+1 }} >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 } js code / / create a Vue instance and get ViewModel var app = new Vue ({el:'# app', data: {list: [], pageSize:3,// page size currentPage:0 / / current page number}, / * data * / mounted () {/ / load json data axios.get ('/ json/student.json') asynchronously {}) .then (function (response) {app.list=response.data });}, / * Auto load function * / methods: {/ / previous page nextPage: function () {if (this.currentPage = = this.pageNumber-1) return; this.currentPage++;}, / / next page prePage: function () {if (this.currentPage = = 0) return; this.currentPage-- }, / / Page number thisPage: function (index) {this.currentPage = index;}}, / * execute trigger function * / computed: {/ / paging data 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); / / take out a page of data}, / / how many pages of pageNumber: function () {if (this.list.length)