基于vue-cli+webpack的demo
项目结构



axios文件夹用来创建axios相关配置:

import axios from 'axios'
import vue from 'vue' axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded' // 请求拦截器
axios.interceptors.request.use(function(config) {
return config;
}, function(error) {
return Promise.reject(error);
})
// 响应拦截器
axios.interceptors.response.use(function(response) {
return response;
}, function(error) {
return Promise.reject(error);
}) // 封装axios的post请求
export function fetch(url, params) {
return new Promise((resolve, reject) => {
axios.post(url, params)
.then(response => {
resolve(response.data);
})
.catch((error) => {
reject(error);
})
})
} export default {
JH_news(url, params) {
return fetch(url, params);
}
}

mock文件夹建立mock数据,配置mock请求:

// 引入mockjs
const Mock = require('mockjs');
// 获取 mock.Random 对象
const Random = Mock.Random;
// mock一组数据
const produceNewsData = function() {
let articles = [];
for (let i = 0; i < 10; i++) {
let newArticleObject = {
title: Random.csentence(5, 30), // Random.csentence( min, max )
thumbnail_pic_s: Random.dataImage('300x250', 'mock的图片'), // Random.dataImage( size, text ) 生成一段随机的 Base64 图片编码
author_name: Random.cname(), // Random.cname() 随机生成一个常见的中文姓名
date: Random.date() // Random.date()指示生成的日期字符串的格式,默认为yyyy-MM-dd;
}
articles.push(newArticleObject)
} return {
articles: articles
}
} // Mock.mock( url, post/get , 返回的数据);
Mock.mock('/news/index', 'post', produceNewsData);

HelloWorld.vue页面首页:

<template>
<div class="index">
<div v-for="(item, key) in newsListShow" :key="key">
<news-cell
:newsDate="item"
></news-cell>
</div>
</div>
</template> <script>
import api from 'js/axios/config'
import NewsCell from './NewsCell.vue' export default {
name: 'index',
data () {
return {
newsListShow: [],
}
},
components: {
NewsCell
},
created() {
this.setNewsApi();
},
methods:{
setNewsApi: function() {
api.JH_news('/news/index', 'type=top&key=123456')
.then(res => {
console.log(res);
this.newsListShow = res.articles;
});
},
}
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped> </style>

NewsCell.vue组件渲染数据:

<template>
<div class="wrap">
<div class="wrap-top">
<h1>{{newsDate.title}}</h1>
<span class="date">{{newsDate.date}}</span>
</div>
<div class="wrap-bottom">
<span class="name">{{newsDate.author_name}}</span>
<img :src="newsDate.thumbnail_pic_s" alt="">
</div>
</div>
</template> <script>
export default {
name: 'NewsCell',
props: {
newsDate: Object
},
data () {
return {
}
},
computed: {
},
methods: {
jumpPage: function () {
window.location.href = this.newsDate.url
}
}
}
</script> <style scoped>
.wrap{
width: 100%;
font-size: 0.3rem;
}
.wrap-top,.wrap-bottom{
display: flex;
align-items: center;
justify-content:space-between;
}
h1{
width: 6rem;
text-align: left;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.date{
width: 4rem;
}
.name{
flex: 1;
}
img{
width: 10rem;
}
</style>

main.js入口文件:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
// 引入mockjs
require('js/mock/mock.js')
Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
运行结果



更为详细的介绍:

Mock.js简易教程,脱离后端独立开发,实现增删改查功能

前后端分离之mockjs实战demo的更多相关文章

  1. JEECG前后端分离UI框架实战版本抢先体验(ng2-admin+Angular4+AdminLTE+WebStorm)

    JEECG前后端分离UI框架实战版本 - 抢先体验 (ng2-admin+Angular4+AdminLTE) 关键词: ng2-admin.Angular4.AdminLTE.Nodejs.Jeec ...

  2. SpringMVC+Spring+mybatis+maven+搭建多模块框架前后端分离开发框架的完整demo,拿走不谢。——猿实战02

            猿实战是一个原创系列文章,通过实战的方式,采用前后端分离的技术结合SpringMVC Spring Mybatis,手把手教你撸一个完整的电商系统,跟着教程走下来,变身猿人找到工作不是 ...

  3. SpringBoot+Vue豆宝社区前后端分离项目手把手实战系列教程01---搭建前端工程

    豆宝社区项目实战教程简介 本项目实战教程配有免费视频教程,配套代码完全开源.手把手从零开始搭建一个目前应用最广泛的Springboot+Vue前后端分离多用户社区项目.本项目难度适中,为便于大家学习, ...

  4. JEECG前后端分离UI框架实战抢先体验(ng2-admin+Angular4+AdminLTE+WebStorm)

    JEECG前后端分离UI框架 (ng2-admin+Angular4+AdminLTE) 关键词: ng2-admin.Angular4.AdminLTE.Nodejs.Jeecg JEECG紧跟技术 ...

  5. 用Spring Security, JWT, Vue实现一个前后端分离无状态认证Demo

    简介 完整代码 https://github.com/PuZhiweizuishuai/SpringSecurity-JWT-Vue-Deom 运行展示 后端 主要展示 Spring Security ...

  6. 前后端分离之mockjs基本介绍

    安装与使用 # 安装 npm install mockjs #使用 Mock var Mock = require('mockjs') var data = Mock.mock({ // 属性 lis ...

  7. SpringBoot+Vue豆宝社区前后端分离项目手把手实战系列教程02---创建后端工程

    本节代码开源地址 代码地址 项目运行截图 搭建后端工程 0.导入sql 在数据库导入 /* Navicat Premium Data Transfer Source Server : localhos ...

  8. 前后端分离(手)-- 使用mock.js(好样的)

    ## 前言: 本篇博文昨天七夕写的,一天下来被虐得体无完肤,苦逼的单身狗只能学习,对!我爱学习,关掉朋友圈,并写了一篇博文发泄发泄.这次写mock.js的使用,能使前后端分离,分离,分离,重要的是说三 ...

  9. SpringBoot,Vue前后端分离开发首秀

    需求:读取数据库的数据展现到前端页面 技术栈:后端有主要有SpringBoot,lombok,SpringData JPA,Swagger,跨域,前端有Vue和axios 不了解这些技术的可以去入门一 ...

随机推荐

  1. [转帖]SAP MES生产执行系统解决方案

    一.SAP MES概述: SAP公司成立于1972年,总部位于德国,是全球最大的企业管理和协同化商务解决方案供应商.全球第三大独立软件供应商.目前,在全球有120多个国家的超过86,000多家用户正在 ...

  2. sql 索引 sql_safe_updates

    为了数据的安全性,mysql有一个安全性设置,sql_safe_updates ,当把这个值设置成1的时候,当程序要对数据进行修改删除操作的时候条件必须要走索引. 刚好现在也碰到了此类问题:网上找了相 ...

  3. iOS记录一常用的方法和语句

    1.当前控制器是否还显示,比较常用于网络请求回来页面已退出 //当前视图控制器是否在显示 +(BOOL)isCurrentViewControllerVisible:(UIViewController ...

  4. 【洛谷P4955 】[USACO14JAN]越野滑雪越野滑雪

    题目链接:ヾ(≧∇≦*)ゝ 对于每一个点可以向它右边的点和下面的点连边,权值就为两个点的高度差 然后再把所有的边按边权从小到大排序,并查集加点 最后判断当前集合是否涵盖所有的航点,如果是,就输出最后一 ...

  5. 【洛谷P1119】灾后重建

    题目大意:给定一个 N 个顶点,M 条边的无向图,每个顶点有一个时间戳,且时间戳大小按照顶点下标大小依次递增,在给定时间 t 时,时间戳严格大于 t 的顶点不能被访问,现在有 Q 次询问,每次询问在给 ...

  6. AIO 开始不定时的抛异常: java.io.IOException: 指定的网络名不再可用

    一天里会抛出几个这样的错误,但发现服务还在正常的运行. java.io.IOException: 指定的网络名不再可用. at sun.nio.ch.Iocp.translateErrorToIOEx ...

  7. laravel 单元测试设置模拟时间

    有时候我们需要对一些超时的逻辑进行测试,需要等待一定的时间来验证超时逻辑是否生效. Carbon 库提供了 setTestNow 方法来设置一个虚拟的当前时间 使用这个特性的前提是:我们的待测试代码利 ...

  8. Apache 的 ab 压测工具快速使用

    ab 是一个 httpd 自带的很好用的压力测试工具,它是 apache bench 命令的缩写.ab 命令会创建多个并发访问线程,模拟多个访问者同时对某一 URL 地址进行访问.可以用来测试 apa ...

  9. webpack打包提取css到独立文件

    将本来镶嵌在bundle.js的css转到外面来,我们需要用到一个插件:extract-text-webpack-plugin 使用方法: 1.安装 npm i extract-text-webpac ...

  10. 在android手机上通过Html5Plus调用java类。

    关于html5plus的资料参考http://www.html5plus.org/ 最近通过html5做手机app,其中涉及到网络通过,必须采用原生的socket,websocket无法满足要求,ht ...