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)境時使用,直接看下面即可步驟
在vite.config.ts進(jìn)行個人配置
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中引入一個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 是一個自增數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動功能(拖動過快失效問題解決方法)
這篇文章主要介紹了vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動功能,文中給大家介紹了鼠標(biāo)移動過快拖動就失效問題的解決方法,需要的朋友可以參考下2018-08-08vue自定義table表如何實(shí)現(xiàn)內(nèi)容上下循環(huán)滾動
這篇文章主要介紹了vue自定義table表如何實(shí)現(xiàn)內(nèi)容上下循環(huán)滾動問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vue3+ts實(shí)現(xiàn)一個表單組件的詳細(xì)代碼
這篇文章主要介紹了vue3+ts實(shí)現(xiàn)一個表單組件的詳細(xì)代碼,確保通過axios調(diào)用后端接口來獲取省市區(qū)和街道數(shù)據(jù),并在選擇省市區(qū)時加載相應(yīng)的街道數(shù)據(jù),需要的朋友可以參考下2024-07-07vue中解決chrome瀏覽器自動播放音頻和MP3語音打包到線上的實(shí)現(xiàn)方法
這篇文章主要介紹了vue中解決chrome瀏覽器自動播放音頻和MP3語音打包到線上的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10