mock

src文件夾下 建立mock.jsjson

const Mock = require('mockjs');
const Random = Mock.Random;
const cityD = require("./json/city.json");
const num = Mock.mock({
      "num|1-500": 500
    })

const productData = function(){
    let countryData = [];
    for(var i=0; i<3; i++){
        let item = {
            areaName: Random.csentence(2, 5),
            countryName: Random.csentence(2, 5),
        }
        countryData.push(item)
    }
    return countryData;
}

const selectData = function(){
    let selectCity = [];
    for(var i=0; i<10; i++){
        let city ={
            value: Random.csentence(2,5),
            label: Random.csentence(2,5)
        }
        selectCity.push(city)
    }
    return selectCity;
}

const scenicData = function() {
    let selectCity = [];
    for(var i=0; i<8; i++){
        let city ={
            src: Random.dataImage('210x230', 'mock的圖片'),
            name: Random.city(),
            ...Mock.mock({
              "num|1-500": 500,
              "stars|1-5": 5
            })
        }
        selectCity.push(city)
    }
    return selectCity;
}

const cityData = function() {
    let cityData = ['華東', '華南', '華北', '華西', '華中'];
    return cityData;
}

Mock.mock('/news/index', 'post', productData); //接口, 請求方式, 返回的數據

Mock.mock('/news/select', 'post', selectData);

Mock.mock('/detail/scenicData', 'post' , scenicData);

Mock.mock('/detail/city', 'post' , cityData)

Mock.mock('/detail/citys', 'post' , cityD)

在main.js中require('./mock')dom

在組件中使用post

相關文章
相關標籤/搜索