欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue項目中使用mock.js的完整步驟

 更新時間:2021年01月12日 14:17:33   作者:求求別Null了  
這篇文章主要給大家介紹了關(guān)于在Vue項目中使用mock.js的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在Vue項目中使用mock.js

  • 開發(fā)工具選擇:Vscode

1. 使用命令行創(chuàng)建vue項目(手動選擇Babel,Router,Vuex)

2. 導(dǎo)入element-ui(為了顯示效果好一點),命令行輸入

npm i element-ui -S

3.在main。js中進行引用

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';//樣式文件一定要引入

Vue.use(ElementUI)

4.新建src/views/main/List.vue使用elememnt-ui中的自定義列模板

<template>
<div>
  <el-table
  :data="tableData"
  style="width: 100%">
  <el-table-column
   label="日期"
   width="180">
   <template slot-scope="scope">
    <i class="el-icon-time"></i>
    <span style="margin-left: 10px">{{ scope.row.date }}</span>
   </template>
  </el-table-column>
  <el-table-column
   label="姓名"
   width="180">
   <template slot-scope="scope">
    <el-popover trigger="hover" placement="top">
     <p>姓名: {{ scope.row.name }}</p>
     <p>住址: {{ scope.row.address }}</p>
     <div slot="reference" class="name-wrapper">
      <el-tag size="medium">{{ scope.row.name }}</el-tag>
     </div>
    </el-popover>
   </template>
  </el-table-column>
  <el-table-column label="操作">
   <template slot-scope="scope">
    <el-button
     size="mini"
     @click="handleEdit(scope.$index, scope.row)">編輯</el-button>
    <el-button
     size="mini"
     type="danger"
     @click="handleDelete(scope.$index, scope.row)">刪除</el-button>
   </template>
  </el-table-column>
 </el-table>
</div>
</template>

<script>
 export default {
  data() {
   return {
    tableData: [{
     date: '2016-05-02',
     name: '王小虎',
     address: '上海市普陀區(qū)金沙江路 1518 弄'
    }, {
     date: '2016-05-04',
     name: '王小虎',
     address: '上海市普陀區(qū)金沙江路 1517 弄'
    }, {
     date: '2016-05-01',
     name: '王小虎',
     address: '上海市普陀區(qū)金沙江路 1519 弄'
    }, {
     date: '2016-05-03',
     name: '王小虎',
     address: '上海市普陀區(qū)金沙江路 1516 弄'
    }]
   }
  },
  methods: {
   handleEdit(index, row) {
    console.log(index, row);
   },
   handleDelete(index, row) {
    console.log(index, row);
   }
  }
 }
</script>

5.router/index.js配置如下

import Vue from 'vue'
import VueRouter from 'vue-router'
//導(dǎo)入組件
import List from '../views/main/List.vue'

Vue.use(VueRouter)

const routes = [
 {
  path: '/',
  name: 'List',
  component: List
 },

]

const router = new VueRouter({
 routes
})

export default router

現(xiàn)在的網(wǎng)頁顯示效果如下

5. 安裝mockjs 和axios

npm install --save-dev mockjs
npm install --save axios

6.新建api/getData.js和request.js

request.js

import axios from 'axios'
const service = axios.create({
  baseURL : "http://localhost:8080",
})
export default service

getData.js

import axios from '../request'
//數(shù)據(jù)列表接口
export const getList = () => axios.get("/list")

7.在src下新建mock/mockServer.js

import Mock from 'mockjs'
//import data from './data.json'

Mock.mock('http://localhost:8080/list', {
  code: 0, data:
  {
    'data|1000': [
      {  
        id: '@id',//隨機id
        name: '@name',//隨機名稱
        nickName: '@last',//隨機昵稱
        phone: /^1[34578]\d{9}$/,//隨機電話號碼
        'age|11-99': 1,//年齡
        address: '@county(true)',//隨機地址
        email: '@email',//隨機郵箱
        isMale: '@boolean',//隨機性別
        createTime: '@datetime',//創(chuàng)建時間
        avatar() {
          //用戶頭像
          return Mock.Random.image('100×100', Mock.Random.color(), '#757575', 'png', this.nickName)
        }
      }
    ]
  }

})

8.在main.js中導(dǎo)入mockServer

import './mock/mockServer'

9.修改src/views/main/List.vue(數(shù)據(jù)獲取與綁定,設(shè)置表格居中)

<template>
 <div>
  <el-table :data="tableData" style="width: 600px;margin: 0 auto">
   <el-table-column label="隨機ID" width="200">
    <template slot-scope="scope">
     <i class="el-icon-time"></i>
     <span style="margin-left: 10px">{{ scope.row.id }}</span>
    </template>
   </el-table-column>
   <el-table-column label="姓名" width="180">
    <template slot-scope="scope">
     <el-popover trigger="hover" placement="top">
      <p>姓名: {{ scope.row.name }}</p>
      <p>住址: {{ scope.row.address }}</p>
      <div slot="reference" class="name-wrapper">
       <el-tag size="medium">{{ scope.row.name }}</el-tag>
      </div>
      <p>郵箱: {{ scope.row.email }}</p>
      <p>性別: {{ scope.row.isMale }}</p>
      <p>昵稱: {{ scope.row.nickName }}</p>
      <p>手機號: {{ scope.row.phone }}</p>
      <p>頭像:</p>
     </el-popover>
    </template>
   </el-table-column>
   <el-table-column label="操作">
    <template slot-scope="scope">
     <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
      >編輯</el-button
     >
     <el-button
      size="mini"
      type="danger"
      @click="handleDelete(scope.$index, scope.row)"
      >刪除</el-button
     >
    </template>
   </el-table-column>
  </el-table>
 </div>
</template>

<script>
import { getList } from "../../api/getData";
export default {
 data() {
  return {
   tableData: [
    //  {
    //   date: "2016-05-02",
    //   name: "王小虎",
    //   address: "上海市普陀區(qū)金沙江路 1518 弄",
    //  },
    //  {
    //   date: "2016-05-04",
    //   name: "王小虎",
    //   address: "上海市普陀區(qū)金沙江路 1517 弄",
    //  },
    //  {
    //   date: "2016-05-01",
    //   name: "王小虎",
    //   address: "上海市普陀區(qū)金沙江路 1519 弄",
    //  },
    //  {
    //   date: "2016-05-03",
    //   name: "王小虎",
    //   address: "上海市普陀區(qū)金沙江路 1516 弄",
    //  },
   ],
  };
 },
 methods: {
  handleEdit(index, row) {
   console.log(index, row);
  },
  handleDelete(index, row) {
   console.log(index, row);
  },

  async getMockList() {
   try {
    const result = await getList();
    //console.log(result);
    if (result.data.code == 0) {
     this.tableData = result.data.data.data;
    }
    //console.log(result.data.data.data);
   } catch (error) {
    console.log(error);
   }
  },
 },

 mounted() {
  this.getMockList();
 },

};
</script>

10.再次運行

鼠標(biāo)放在姓名上,會有更多信息顯示

顯示測試的數(shù)據(jù)1000條

分頁就懶得做了。。。。

總結(jié)

到此這篇關(guān)于Vue項目中使用mock.js的文章就介紹到這了,更多相關(guān)項目使用mock.js內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue3使用vue-qrcode-reader實現(xiàn)掃碼綁定設(shè)備功能(推薦)

    Vue3使用vue-qrcode-reader實現(xiàn)掃碼綁定設(shè)備功能(推薦)

    本文介紹了在Vue3中使用vue-qrcode-reader版本5.5.7來實現(xiàn)移動端的掃碼綁定設(shè)備功能,用戶通過掃描二維碼自動獲取設(shè)備序列號,并填充到添加設(shè)備界面,完成設(shè)備綁定的全過程,包含ScanCode.vue和DeviceAdd.vue兩個主要界面的實現(xiàn)方式
    2024-10-10
  • vue 2.x 中axios 封裝的get 和post方法

    vue 2.x 中axios 封裝的get 和post方法

    本文通過實例代碼給大家介紹了vue 2.x 中axios 封裝的get 和post方法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2018-02-02
  • vue router 源碼概覽案例分析

    vue router 源碼概覽案例分析

    這篇文章主要介紹了vue router 源碼概覽的案例分析,本文通過實例代碼案例分析給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-10-10
  • 詳解在vue-cli項目下簡單使用mockjs模擬數(shù)據(jù)

    詳解在vue-cli項目下簡單使用mockjs模擬數(shù)據(jù)

    這篇文章主要介紹了詳解在vue-cli項目下簡單使用mockjs模擬數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • vue 右鍵菜單插件 簡單、可擴展、樣式自定義的右鍵菜單

    vue 右鍵菜單插件 簡單、可擴展、樣式自定義的右鍵菜單

    這篇文章主要介紹了vue 右鍵菜單插件 簡單、可擴展、樣式自定義的右鍵菜單,本文給大家分享個例子,給大家及時的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2018-11-11
  • JavaScript獲取echart曲線上任意點位的值詳解

    JavaScript獲取echart曲線上任意點位的值詳解

    這篇文章主要為大家介紹了JavaScript獲取echart曲線上任意點位的值詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Vue3使用el-form嵌套el-table進行單條數(shù)據(jù)的表單校驗功能

    Vue3使用el-form嵌套el-table進行單條數(shù)據(jù)的表單校驗功能

    在實際開發(fā)過程中,我們經(jīng)常需要處理表格中的表單數(shù)據(jù),比如在編輯表格中的某一行數(shù)據(jù)時進行校驗,本文給大家介紹了Vue3使用el-form嵌套el-table進行單條數(shù)據(jù)的表單校驗功能,文中有相關(guān)的代碼供大家參考,需要的朋友可以參考下
    2024-08-08
  • vue3實現(xiàn)H5表單驗證組件的示例

    vue3實現(xiàn)H5表單驗證組件的示例

    本文主要介紹了vue3實現(xiàn)H5表單驗證組件的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 針對Vue路由history模式下Nginx后臺配置操作

    針對Vue路由history模式下Nginx后臺配置操作

    這篇文章主要介紹了針對Vue路由history模式下Nginx后臺配置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • 詳解Vue-cli webpack移動端自動化構(gòu)建rem問題

    詳解Vue-cli webpack移動端自動化構(gòu)建rem問題

    這篇文章主要介紹了詳解Vue-cli webpack移動端自動化構(gòu)建rem問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論