mock.js 和引入本地json文件 学习笔记

mock.js 


https://blog.csdn.net/Alcantara/article/details/78917846

Vue2.0使用axios引入本地JSON文件踩的坑


今天学习Vue2.0使用axios引入本地JSON文件踩的坑。

1.如何引入axios,import、prototype
  本地JSON文件需放在static文件夹之下。(以及图片文件)。参见http://blog.csdn.net/Mr_YanYan/article/details/78783091
2.response是个Object对象,但是response.data才是本地JSON文件的对象
3.response.data已经是一个Object类型;原生JS写Ajax返回的response是string类型
4.JSON文件不得有注释,否则返回的是string
5.JSON文件如果有注释,JSON.parse报错含有非法字符/。JSON文件本来就是对象,再用
JSON.parse(),会报错含有非法字符o

先奉上根组件script:

[javascript] view plain copy
  1. export default {  
  2.     name:"app",  
  3.     data() {  
  4.       return {  
  5.         itemList:[]  
  6.       }  
  7.     },  
  8.     mounted() {  
  9.       this.getData();  
  10.     },  
  11.     methods:{  
  12.       getData() {  
  13.         this.$axios.get("../static/productList.json",{id:page1}).then(response => {  
  14.           this.itemList = response.data.result.list;  
  15.         });  
  16.       }  
  17.     }  
  18.   } 

https://facebook.github.io/jest/docs/en/mock-functions.html

猜你喜欢

转载自blog.csdn.net/zgpeterliu/article/details/80109250