vue实战-mockjs模拟数据

使用mock模拟数据:
npm i mockjs

request.js
baseURL: '/mock',

在src目录下创建文件夹mock
在mock中创建mockServer.js用来模拟数据

//引入mockjs插件
//这是对象,必须要大写
import Mock from 'mockjs'
//引入json文件
//webpack下默认暴露的文件格式是:图片和json数据格式,所以这里的json文件不需要暴露
import banner from './banner.json'
import floor from './floor.json'

//使用Mock对象中的mock方法模拟服务器返回的值
//传入两个参数:第一个参数是:请求地址,第二个参数是请求数据
Mock.mock('/mock/banner',{code:200,data:banner})
Mock.mock('/mock/floor',{code:200,data:floor})
[
  {
    "id": "1",
    "imgUrl": "/images/banner1.jpg"
  },
  {
    "id": "2",
    "imgUrl": "/images/banner2.jpg"
  },
  {
    "id": "3",
    "imgUrl": "/images/banner3.jpg"
  },
  {
    "id": "4",
    "imgUrl": "/images/banner4.jpg"
  }
]

main.js中引入才能读取到

import "@/mock/mockServer"

api.js

export function getBannerList() {
    return request({
      url: '/banner',
      method: 'get',
    })
  }

.vue

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <p>
      11111For a guide and recipes on how to configure / customize this project,<br>
      check out the
    </p>
    <h3>Installed CLI Plugins</h3>
    <ul>
      <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li>
      <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank" rel="noopener">eslint</a></li>
    </ul>
    <h3>Essential Links</h3>
    <ul>
      <li><a href="https://vuejs.org" target="_blank" rel="noopener">Core Docs</a></li>
      <li><a href="https://forum.vuejs.org" target="_blank" rel="noopener">Forum</a></li>
      <li><a href="https://chat.vuejs.org" target="_blank" rel="noopener">Community Chat</a></li>
      <li><a href="https://twitter.com/vuejs" target="_blank" rel="noopener">Twitter</a></li>
      <li><a href="https://news.vuejs.org" target="_blank" rel="noopener">News</a></li>
    </ul>
    <h3>Ecosystem</h3>
    <ul>
      <li><a href="https://router.vuejs.org" target="_blank" rel="noopener">vue-router</a></li>
      <li><a href="https://vuex.vuejs.org" target="_blank" rel="noopener">vuex</a></li>
      <li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank" rel="noopener">vue-devtools</a></li>
      <li><a href="https://vue-loader.vuejs.org" target="_blank" rel="noopener">vue-loader</a></li>
      <li><a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">awesome-vue</a></li>
    </ul>
  </div>
</template>

<script>
import { getRoles, getBannerList } from '@/api/api'
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data() {
    return {
      pageIndex: false,
      pageSize: false,
      dataList: [],
    }
  },
  created() {
    console.log(12312);
    this.getDataList();
    this.getBanner();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      getRoles({
        'page': this.pageIndex,
        'limit': this.pageSize,
      }).then((data) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    async getBanner(){
      var res = await getBannerList();
      console.log('res', res);
    }

    // try {
    //   let res = await this.$api.data_getDepartOptions()
    //   this.loading = false
    //   this.departOptions = res.result
    // } catch (e) {
    //   this.loading = false
    // }
  },
  
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

https://blog.csdn.net/m0_45219210/article/details/124326127
https://blog.csdn.net/bying666/article/details/127037897

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值