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:
- export default {
- name:"app",
- data() {
- return {
- itemList:[]
- }
- },
- mounted() {
- this.getData();
- },
- methods:{
- getData() {
- this.$axios.get("../static/productList.json",{id:page1}).then(response => {
- this.itemList = response.data.result.list;
- });
- }
- }
- }
https://facebook.github.io/jest/docs/en/mock-functions.html