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

在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>
打印效果如下:
如果想使用隨機(jī)數(shù)可以看接下來的步驟
如果只要隨機(jī)數(shù)則直接生成即可

想要隨機(jī)數(shù)在return中放入隨機(jī)條件即可。
如果想要用隨機(jī)數(shù)中的圖片就需要從mockjs中引入一個(gè)Random方法
在頁面上進(jìn)行循環(huán):
<template>
<div v-for="(item,index) in list" :key="index">
<img :src="item.image" alt="">
<p>{{item.id}}</p>
</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(list.value =lis);
});
});
return {
nc,
list,
};
},
};
</script>
<style scoped>
</style>
這里的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)功能,文中給大家介紹了鼠標(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)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
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ì)代碼,確保通過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配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
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

