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

Vue3+Vite項(xiàng)目使用mockjs隨機(jī)模擬數(shù)據(jù)

 更新時(shí)間:2023年01月26日 13:02:15   作者:Nanchen_42  
這篇文章主要介紹了Vue3+Vite項(xiàng)目使用mockjs隨機(jī)模擬數(shù)據(jù),需要的朋友可以參考下

在vite中使用mockjs進(jìn)行模擬數(shù)據(jù),需要借助新的依賴進(jìn)行使用

一、安裝mockjs

yarn add mockjs -S 或 npm i mockjs -D

二、安裝vite-plugin-mock

npm i vite-plugin-mock -D

三、在src/mock/source文件夾下創(chuàng)建user.ts

請(qǐng)?zhí)砑訄D片描述

在index.vue中放入以下內(nèi)容:

import { MockMethod } from 'vite-plugin-mock'

export default [
  {
    url: '/api/getUserInfo', // 注意,這里只能是string格式
    method: 'get',
    response: () => {
      return {
        menusList: [{
          id: '1',
          title: '南辰',
          subMenuList: [
            {
              id: '11',
              title: '南',
              path: '/user/nan'
            },
            {
              id: '12',
              title: '小',
              path: '/user/xiao'
            },
            {
              id: '13',
              title: '辰',
              path: '/user/chen'
            }
          ]
        }, {
          id: '2',
          title: '希',
          subMenuList: [
            {
              id: '21',
              title: '玩游戲',
              path: '/user/play'
            }
          ]
        }]
      }
    }
  }
] as MockMethod[] // 這里其實(shí)就是定義數(shù)據(jù)格式的,不了解的同學(xué)可以參考typescript的官方文檔

四、開發(fā)環(huán)境配置

如果只是本地開發(fā)環(huán)境時(shí)使用,直接看下面即可步驟

在vite.config.ts進(jìn)行個(gè)人配置

import { viteMockServe } from 'vite-plugin-mock'

export default defineConfig({
  plugins: [
    viteMockServe({
      mockPath: "./src/mock/source", // 解析剛剛user.ts的位置
      localEnabled: true // 是否開啟開發(fā)環(huán)境
    })
  ]
})

在頁面中引入

<template>
  <div>{{name.name}}</div>
  <div>{{nc}}</div>
</template>

<script lang='ts'>
import { useRoute } from "vue-router"; //引入路由組件
import { onMounted, ref } from "vue";
import axios from "axios";
export default {
  setup() {
    const nc = ref("");
    onMounted(() => {
      axios.get("/api/getUserInfo").then((res) => {
        console.log(res);
        nc.value = res.data.menusList[0].title;
        console.log(nc.value);
      });
    });
    const $route = useRoute();
    const name = $route.query;
    return {
      name,
      nc,
    };
  },
};
</script>
<style scoped>
</style>

打印效果如下:
請(qǐng)?zhí)砑訄D片描述

如果想使用隨機(jī)數(shù)可以看接下來的步驟

如果只要隨機(jī)數(shù)則直接生成即可
請(qǐng)?zhí)砑訄D片描述
在這里插入圖片描述

想要隨機(jī)數(shù)在return中放入隨機(jī)條件即可。

如果想要用隨機(jī)數(shù)中的圖片就需要從mockjs中引入一個(gè)Random方法
在這里插入圖片描述
在頁面上進(jìn)行循環(huán):

&lt;template&gt;
  &lt;div v-for="(item,index) in list" :key="index"&gt;
   &lt;img :src="item.image" alt=""&gt;
   &lt;p&gt;{{item.id}}&lt;/p&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script lang='ts'&gt;
import { useRoute } from "vue-router"; //引入路由組件
import { onMounted, ref } from "vue";
import axios from "axios";
export default {
  setup() {
    const list = ref("");
    onMounted(() =&gt; {
      axios.get("/api/getUserInfo").then((res) =&gt; {
        console.log(res);
        let lis = res.data.list;
        console.log(list.value =lis);
      });
    });

    return {
      nc,
      list,
    };
  },
};
&lt;/script&gt;
&lt;style scoped&gt;
&lt;/style&gt;

這里的Random.image()方法是從官網(wǎng)上拿下來用的
在這里插入圖片描述
效果如下:
在這里插入圖片描述

實(shí)現(xiàn)隨機(jī)不同的圖片+字段

import { MockMethod } from 'vite-plugin-mock'
export default [
  {
    url: '/api/getUserInfo', // 注意,這里只能是string格式
    method: 'get',
    response: () => {
      return {
        'list|1-10': [{
          // 屬性 id 是一個(gè)自增數(shù),起始值為 1,每次增 1
          'id|+1': 1,
        /*   image: Random.image() */
        "title": "@ctitle",
        "color":'@color',
        "image":"@image('','@color')"
        }],
      }
    }
  }
] as MockMethod[] 

index.vue

<template>
<div v-for="(item,index) in list" :key="index">
 <img :src="item.image" alt="">
 {{item.title}}
 </div>
</template>

<script lang='ts'>
import { useRoute } from "vue-router"; //引入路由組件
import { onMounted, ref } from "vue";
import axios from "axios";

export default {
  setup() {
    const list = ref("");
    onMounted(() => {
      axios.get("/api/getUserInfo").then((res) => {
        console.log(res);
        let lis = res.data.list;
        console.log(lis);
        console.log(list.value = lis);
      });
    });
    return {
      list,
    };
  },
};
</script>
<style scoped>
</style>

在這里插入圖片描述
效果如下:
在這里插入圖片描述

到此這篇關(guān)于Vue3+Vite項(xiàng)目使用mockjs隨機(jī)模擬數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Vue3+Vite項(xiàng)目使用mockjs模擬數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能(拖動(dòng)過快失效問題解決方法)

    vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能(拖動(dòng)過快失效問題解決方法)

    這篇文章主要介紹了vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能,文中給大家介紹了鼠標(biāo)移動(dòng)過快拖動(dòng)就失效問題的解決方法,需要的朋友可以參考下
    2018-08-08
  • vue自定義table表如何實(shí)現(xiàn)內(nèi)容上下循環(huán)滾動(dòng)

    vue自定義table表如何實(shí)現(xiàn)內(nèi)容上下循環(huán)滾動(dòng)

    這篇文章主要介紹了vue自定義table表如何實(shí)現(xiàn)內(nèi)容上下循環(huán)滾動(dòng)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue中過濾器定義以及使用方法實(shí)例

    Vue中過濾器定義以及使用方法實(shí)例

    過濾器的功能是對(duì)要顯示的數(shù)據(jù)進(jìn)行格式化后再顯示,其并沒有改變?cè)镜臄?shù)據(jù),只是產(chǎn)生新的對(duì)應(yīng)的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Vue中過濾器定義以及使用方法的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • vue2中seo時(shí)使用vue-meta-info的方法

    vue2中seo時(shí)使用vue-meta-info的方法

    這篇文章主要介紹了vue2中seo時(shí)使用vue-meta-info,本文通過實(shí)例代碼給大家詳細(xì)講解,文末給大家補(bǔ)充介紹了vue seo管理 vue-meta-info 動(dòng)態(tài)設(shè)置meta和title的相關(guān)知識(shí),需要的朋友可以參考下
    2022-10-10
  • vue3+ts實(shí)現(xiàn)一個(gè)表單組件的詳細(xì)代碼

    vue3+ts實(shí)現(xiàn)一個(gè)表單組件的詳細(xì)代碼

    這篇文章主要介紹了vue3+ts實(shí)現(xiàn)一個(gè)表單組件的詳細(xì)代碼,確保通過axios調(diào)用后端接口來獲取省市區(qū)和街道數(shù)據(jù),并在選擇省市區(qū)時(shí)加載相應(yīng)的街道數(shù)據(jù),需要的朋友可以參考下
    2024-07-07
  • vue中的axios配置及接口請(qǐng)求路徑api配置

    vue中的axios配置及接口請(qǐng)求路徑api配置

    這篇文章主要介紹了vue中的axios配置及接口請(qǐng)求路徑api配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue .sync修飾符的使用詳解

    vue .sync修飾符的使用詳解

    這篇文章主要介紹了vue .sync修飾符的使用,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • vue中解決chrome瀏覽器自動(dòng)播放音頻和MP3語音打包到線上的實(shí)現(xiàn)方法

    vue中解決chrome瀏覽器自動(dòng)播放音頻和MP3語音打包到線上的實(shí)現(xiàn)方法

    這篇文章主要介紹了vue中解決chrome瀏覽器自動(dòng)播放音頻和MP3語音打包到線上的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 詳解Vue中的scoped及穿透方法

    詳解Vue中的scoped及穿透方法

    這篇文章主要介紹了Vue中的scoped及穿透方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Vue使用Canvas生成隨機(jī)大小且不重疊圓

    Vue使用Canvas生成隨機(jī)大小且不重疊圓

    Canvas是HTML5中新增的元素,專門用來繪制圖形,下面這篇文章主要給大家介紹了關(guān)于Vue使用Canvas生成隨機(jī)大小且不重疊圓的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11

最新評(píng)論