var Mock = require('mockjs') var data = Mock.mock({ // 屬性 list 的值是一個數組,其中含有 1 到 10 個元素 'list|1-10': [{ // 屬性 id 是一個自增數,起始值爲 1,每次增 1 'id|+1': 1 }] }) // 輸出結果 console.log(JSON.stringify(data, null, 4))
mock.js的做用,從它文檔的首頁介紹即可以略知一二:前端
var Random = Mock.Random Random.domain() // "nhou.org.cn"
var Random = Mock.Random Random.ip() // "74.97.41.159"
Random.url() // "news://wrmt.na/rbcgbws"
var Random = Mock.Random Random.province() //"海南省"
var Random = Mock.Random Random.city() // "澳門半島"
var Random = Mock.Random Random.city(true) // "廣東省 廣州市"
var Random = Mock.Random Random.csentence() // "會候權以解包黨心要按總場火義國而片精。"
Random.csentence(5) // "文鬥領拉米。"
Random.csentence(3, 5) // "住驗住"
var Random = Mock.Random Random.cparagraph() // "電力速率離老五準東其引是外適只王。體區先手天裏己車發很指一照委爭本。究利天易里根幹鐵多而提造幹下志維。級素一門件一壓路低表且太馬。"
Random.cparagraph(2)
Random.cparagraph(1, 3)
var Random = Mock.Random Random.rgba() // "rgba(122, 121, 242, 0.13)"
Random.date('yyyy-MM-dd') // "1975-04-27" Random.date('yy-MM-dd') // "00-01-08"
Random.time() // "05:06:06"
Random.image('200x100', '#4A7BF7', 'Hello')
Random.cname() // "黃秀英"
Random.cfirst() // "龍"
Random.clast() // "秀英"
import Vue from 'vue' //註冊Vue-Resource插件,這樣咱們就能夠Vue.http.get(URL)去發出Ajax請求了 import VueResource from 'vue-resource' Vue.use(VueResource) var Mock = require('mockjs') Mock.mock('/penghuwan.com/', 'get', { // 屬性 list 的值是一個數組,其中含有 1 到 10 個元素 'list|1-10': [{ // 屬性 id 是一個自增數,起始值爲 1,每次增 1 'id|+1': 1 }] }) // 輸出結果 Vue.http.get('/penghuwan.com/').then( response => { console.log(response.body) } )
demo:vue
<template> <div id="app"> <template v-for ="(item, index) in articles"> <div :key="index"> <h1 class="title">{{ item.title }}</h1> <div> {{ item.content }} </div> <div> <p class="time">{{ '發表時間:' + item.time}}</p> <p class="location">{{ '發表地址: ' + item.location }}</p> </div> </div> </template> </div> </template> <script> import { mapGetters, mapActions } from 'vuex' export default { mounted: function () { this.fetchData() }, methods: { ...mapActions({ fetchData: 'fetchArticlesData' }) }, computed: { ...mapGetters({ articles: 'getArticles' }) } } </script> <style scoped> #app div{ border: 1px solid gray; padding: 10px; margin: 10px; overflow: hidden; } #app p{ margin: 0px; } .title{ font-size: 16px; } .time{ float: left; } .location{ float: right; } </style>
import Vue from 'vue' import Vuex from 'vuex' import VueResource from 'vue-resource' import App from './app' import article from './module.js' Vue.use(Vuex) Vue.use(VueResource) // 調用mock的API,使Ajax可以捕獲隨機數據 require('./mock.js') // 建立Vuex的store const store = new Vuex.Store({ modules: { article } }) new Vue({ el: '#app', template: '<App />', store: store, components: { App } })
var Mock = require('mockjs') var Random = Mock.Random const produceData = function () { let articles = [] for (let i = 0; i < 10; i++) { let newArticleObject = { title: Random.csentence(5), content: Random.cparagraph(5, 7), time: Random.date() + ' ' + Random.time(), location: Random.city() } articles.push(newArticleObject) } return { articles: articles } } // 第三個參數能夠是對象也能夠是返回對象的函數 Mock.mock('/article', 'get', produceData)
import Vue from 'vue' const RESQUEST_ARTICLES = 'RESQUEST_ARTICLES' export default { state: { articles: [] }, getters: { getArticles: state => state.articles }, actions: { fetchArticlesData (context) { context.commit(RESQUEST_ARTICLES) } }, mutations: { [RESQUEST_ARTICLES] (state) { Vue.http.get('/article').then( response => { let data = response.body state.articles = data.articles } ) } } }
【完】