vue项目中使用mockjs+axios模拟后台数据返回

import axios from 'axios' 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)
})
import Mock from 'mockjs' const Random = Mock.Random // mock需要给三个参数,url(与axios请求是传的url一致,我这个是本地启动的项目就直接用本地域名了)
// 请求类型: get post...其他看文档
// 数据处理函数,函数需要return数据
Mock.mock('http://localhost:8081/test/city', 'get', () => {
let citys = []
for (let i = 0; i < 10; i++) {
let obj = {
id: i+1,
city: Random.city(),
color: Random.color()
}
citys.push(obj)
}
return {cityList: citys}
})
// post请求,带参数,参数会在data中返回,会返回url,type,body三个参数,可以把data打印出来看看
Mock.mock('http://localhost:8081/test/cityInfo', 'post', (data) => {
// 请求传过来的参数在body中,传回的是json字符串,需要转义一下
const info= JSON.parse(data.body)
return {
img: Random.image('200x100', '#4A7BF7', info.name)
}
})
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import routes from './router'
import './mock/mock.js'
import axios from 'axios'
import '../config/axios' // 将axios挂载到Vue实例,在组件中可以直接使用
Vue.prototype.$axios = axios
Vue.config.productionTip = false Vue.use(VueRouter)
const router = new VueRouter({
routes,
strict: process.env.NODE_ENV !== 'production'
}) new Vue({
router,
render: h => h(App)
}).$mount('#app')
<template>
<div>
test
<button @click="clickMe">获取城市</button>
<ul class="city_container">
<li class="city_item" v-for="item in cityList" :key="item.id" @click="getCityInfo(item.city)">
<a href="javascript:void(0)" :style="{color: item.color}">{{item.city}}</a>
</li>
</ul>
<img :src="img" alt="">
</div>
</template>
<script>
export default {
name: 'test',
components: { },
data() {
return {
cityList: [],
img: ''
}
},
methods: {
clickMe () {
// 这里请求的地址要和mock中定义的请求地址一致
this.$axios.get('http://localhost:8081/test/city').then(res => {
console.log(77, res)
if (res.data) {
this.cityList = res.data.cityList
}
})
}, getCityInfo (name) {
this.$axios.post('http://localhost:8081/test/cityInfo', {
name: name
}).then(res => {
console.log(88, res)
if (res.data) {
this.img = res.data.img
}
})
}
} }
</script>
<style scoped>
.city_item {
list-style: none;
float: left;
border: 1px solid orange;
width: auto;
height: 50px;
line-height: 50px;
padding: 0 5px;
border-right: none;
}
.city_container :last-of-type {
border-right: 1px solid orange;
}
.city_container .city_item a {
text-decoration: none;
border: none;
}
</style>


vue项目中使用mockjs+axios模拟后台数据返回的更多相关文章
- Vue项目中使用Vuex + axios发送请求
本文是受多篇类似博文的影响写成的,内容也大致相同.无意抄袭,只是为了总结出一份自己的经验. 一直以来,在使用Vue进行开发时,每当涉及到前后端交互都是在每个函数中单独的写代码,这样一来加大了工作量,二 ...
- vue项目中使用mockjs模拟接口返回数据
Mock.js 是一个模拟数据生成器,利用它,可以拦截ajax请求,直接模拟返回数据,这样前后端只要约定好数据格式,前端就不需要依赖后端的接口,可以直接使用模拟的数据了. 网上介绍mock的教程也较多 ...
- express+mockjs实现模拟后台数据发送
前言: 大多数时候,前端会和后端同时进行开发,即在我们开发完页面的时候,很可能还不能立马进入联调阶段,这个时候,为了保证我们接口的有效性和代码的功能完整,我们可能需要模拟数据. 模拟数据方法 1.通过 ...
- Vue项目中引入mockjs
前提:创建好的vue项目 前言: 为什么引入mockjs:为了实现前后端分离,开发工作可以异步进行 其他工具:axios 一般的前后端交互过程:前端 --> ajax请求 --> 网络协议 ...
- vue项目中多个组件之间传递数据
//父组件<template> <div> <div style="float: left"> <input-data :city=&qu ...
- 超简单本地mock假数据测试,模拟后台数据返回必杀技
温馨提示:急性子可以直接拉到最后观看方法步骤. 什么是mock? mock就是在开发过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试开发的方法. 使用mock有什么好处? ...
- 在vue项目中的axios使用配置记录
默认vue项目中已经安装axios,基于element-ui开发,主要记录配置的相关. axiosConfig.js import Vue from 'vue' import axios from ' ...
- vue项目中关于axios的简单使用
axios介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中 官方仓库:https://github.com/axios/axios 中文文档:htt ...
- 浅谈 Axios 在 Vue 项目中的使用
介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 特性 它主要有如下特性: 浏览器端发起XMLHttpRequests请求 Node端发起http ...
随机推荐
- 微信小程序之条件判断
前文: 今天踩了一下午的坑,但是确实很简单的问题. 我说一下需求:扫描商品的二维码,从而判断,同一个二维码不可多次扫描: 点击扫一扫 会在灰色区域展示 扫描的商品信息,比如商品名称,商品码等,但是我们 ...
- Oracle 12C 物理Standby 主备切换switchover
Oracle 12C 物理Standby 主备切换switchover Oracle 12C 物理Standby 主备切换switchover Table of Contents 1. 简述 2. 切 ...
- 20 Django REST Framework 更改PUT/PATCH/DELETE的传参字段,默认为pk
01-lookup_field 默认为 lookup_field='pk' 更改后的效果:
- linux系统安装zint
背景: 今天代码拉下了发现启动时报错,一看原来是同事用了zint的gem,我又没安,然后花了点时间解决,但其中踩了几次坑,所以打算记录下: 一.zint开源库的介绍 zint 是一个开源的条码编码库, ...
- win + T 快捷键
和 alt + tab 不同,win + T会在 在任务栏间的程序间进行switch
- 从 ssh private key 中重新生成 public key
Use the -y option to ssh-keygen: ssh-keygen -f ~/.ssh/id_rsa -y > ~/.ssh/id_rsa.pub From the 'man ...
- HBase 参考信息
Apache HBase Region Splitting and Merging https://blog.cloudera.com/apache-hbase-region-splitting-a ...
- 【一个开发设想】开发一个游戏向时间管理APP
什么是游戏向时间管理呢? 首先我们要做的是时间管理.为了更好地利用时间,摆脱拖延症. 其次是游戏向.就是利用主线任务.支线任务.每日任务的方式展现,一般来讲,没人会讨厌玩游戏.更何况玩这个“游戏”是为 ...
- vim编辑器详解(week1_day3)
vi编辑器 作用:编辑文本文件中的内容的工具 命令历史 末行模式中,以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中 ...
- 自然数幂和(递推式k^2方法)
先用这个方法顶一下!