当前位置: 首页 > news >正文

做网站需要接口么外链平台

做网站需要接口么,外链平台,网站首页自动下拉广告,工业园区管委会网站建设方案vite-plugin-mock前端自行模拟接口返回数据的插件 安装导入、配置(vite.config.js)使用目录结构/mock/user.js具体在页面请求中的使用 注意事项 中文文档:[https://gitcode.com/vbenjs/vite-plugin-mock/blob/main/README.zh_CN.md) 参考其他…

vite-plugin-mock前端自行模拟接口返回数据的插件

  • 安装
  • 导入、配置(vite.config.js)
  • 使用
    • 目录结构
    • /mock/user.js
    • 具体在页面请求中的使用
  • 注意事项

中文文档:[https://gitcode.com/vbenjs/vite-plugin-mock/blob/main/README.zh_CN.md)
参考其他博主文档:
【博主:火山犬】三分钟教你怎么用vite-plugin-mock和mockjs
【博主:木子静静】vue3:vite-plugin-mock插件的使用过程
【博主:木子静静】vue3:vite-plugin-mock插件的使用过程
【博主:wade3po】vite-plugin-mock使用
【博主:哥很冷漠】在vite(vue)项目中使用mockjs

以下是我自己的简单应用记录:

安装

npm i  mockjs -S
npm i vite-plugin-mock -D

导入、配置(vite.config.js)

import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { viteMockServe } from 'vite-plugin-mock'// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {// 根据当前工作目录中的 `mode` 加载 .env 文件// 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。const env = loadEnv(mode, process.cwd(), '')return {// vite 配置// base:'./',define: {__APP_ENV__: JSON.stringify(env.APP_ENV),},plugins: [vue(),viteMockServe({mockPath: 'mock',localEnabled: true, // 开发打包开关 true时打开mock  false关闭mockprodEnabled: false, //生产环境下为false,这样就不会被打包到生产包中}),],resolve: {alias: {'@': fileURLToPath(new URL('./src', import.meta.url))}},server:{proxy: {'/api': {target: 'http://locallhost:8080',changeOrigin: true,rewrite: (path) => path.replace(/^\/api/, '')}}}}
})

使用

目录结构

在这里插入图片描述
注意:这里,mock文件夹的创建位置及文件名需要和前面vite.config.js里的mockPath配置的一致,mock底下的文件名没有特殊要求

/mock/user.js

这里你可以自己根据自己需要划分的api模块进行文件命名,没有具体要求。不划分模块,全写一起也是可以的

function createUserList(){return [{userId:1,avatar:"",username:"admin",password:'123456',desc:'平台管理员',roles:['平台管理员'],routes:['home'],token:"Admin Token"},{userId:2,avatar:"",username:"system",password:'123456',desc:'系统管理员',roles:['系统管理员'],routes:['system'],token:"System Token"}]
}export default [// 用户登录接口{url:'/api/user/login',method:'post',response:(({body})=>{const {username,password} = bodyconst checkUser = createUserList().find((item)=>{return item.username === username && item.password === password})if(!checkUser){return {code:201,data:{message:'账号或密码不正确'}}}let {token} = checkUserreturn {code:200,data:{token}}})},//获取用户信息{url:'/api/user/userInfo',method:'post',response:(({body})=>{const {token} = bodyconst checkUser = createUserList().find((item)=>{return item.token === token })if(!checkUser){return {code:201,data:{message:'token失效'}}}return {code:200,data:{checkUser}}})}
]

具体在页面请求中的使用

<template><h1>vite-plugin-mock插件</h1><p>模拟请求到的数据:</p><p>token:</p><p>{{ token }}</p><p>userInfo:</p><p>{{ userInfo }}</p>
</template><script setup>
import { ref } from 'vue'
import axios from 'axios';
let token = ref(null);
let userInfo = ref(null);axios.post('/api/user/login',{username:'admin',password:'123456'}).then(({data: {data}}) => {console.log(data,'token---------------');if(data && data.token) {token = data.token;axios.post('/api/user/userInfo',{token:token}).then(({data:{data}}) => {console.log(data,'userInfo---------------');userInfo = data;})}
})
</script>

注意事项

注意不要把mock打包进生产包里了,具体看vite.config.js里的配置
我觉得这个只适合在项目前期,项目开发环境还没有健全,没办法调测试接口进行开发的时候用;当项目开发环境健全的时候,我们直接调开发环境接口进行开发,这个mock可以直接不要


文章转载自:
http://dinncometz.tpps.cn
http://dinnconene.tpps.cn
http://dinncocutch.tpps.cn
http://dinncoeunuchize.tpps.cn
http://dinncofluffy.tpps.cn
http://dinncoconvention.tpps.cn
http://dinncohandtector.tpps.cn
http://dinncohyperthermal.tpps.cn
http://dinncoprecursor.tpps.cn
http://dinncocomprador.tpps.cn
http://dinncolentiform.tpps.cn
http://dinncopyrogallic.tpps.cn
http://dinncopreachment.tpps.cn
http://dinncopolyhedra.tpps.cn
http://dinncovane.tpps.cn
http://dinncodisengagement.tpps.cn
http://dinncoprotozoa.tpps.cn
http://dinncolibertinage.tpps.cn
http://dinncoelectrology.tpps.cn
http://dinncoolim.tpps.cn
http://dinncoprecipitate.tpps.cn
http://dinncothanatopsis.tpps.cn
http://dinncoerythrochroism.tpps.cn
http://dinncovocationalize.tpps.cn
http://dinncoinculpatory.tpps.cn
http://dinncotetramethyllead.tpps.cn
http://dinncocodein.tpps.cn
http://dinncohologram.tpps.cn
http://dinncorubbingstone.tpps.cn
http://dinncodrooping.tpps.cn
http://dinncoimperception.tpps.cn
http://dinncosplotchy.tpps.cn
http://dinncosanify.tpps.cn
http://dinncoovertechnologize.tpps.cn
http://dinncomenopausic.tpps.cn
http://dinncoabashed.tpps.cn
http://dinncocoinstantaneous.tpps.cn
http://dinncotryout.tpps.cn
http://dinncorecap.tpps.cn
http://dinncoophiolater.tpps.cn
http://dinncoinconsecutive.tpps.cn
http://dinncokokobeh.tpps.cn
http://dinncosolidify.tpps.cn
http://dinncoresurrect.tpps.cn
http://dinncounivalent.tpps.cn
http://dinncothrenetic.tpps.cn
http://dinncotelesat.tpps.cn
http://dinncopunctuative.tpps.cn
http://dinncopseudo.tpps.cn
http://dinncolunary.tpps.cn
http://dinncowasherette.tpps.cn
http://dinncostrawworm.tpps.cn
http://dinncoiritis.tpps.cn
http://dinncomunt.tpps.cn
http://dinncomyocardia.tpps.cn
http://dinncocruciate.tpps.cn
http://dinncoarmy.tpps.cn
http://dinncogenearch.tpps.cn
http://dinncoteemless.tpps.cn
http://dinncoshintoist.tpps.cn
http://dinncoloyally.tpps.cn
http://dinncoblubbery.tpps.cn
http://dinncotaxite.tpps.cn
http://dinncophthiriasis.tpps.cn
http://dinncoferine.tpps.cn
http://dinncodoncher.tpps.cn
http://dinncothermite.tpps.cn
http://dinncobristling.tpps.cn
http://dinncoinbuilt.tpps.cn
http://dinncoaliesterase.tpps.cn
http://dinncotumefacient.tpps.cn
http://dinncoorthomolecular.tpps.cn
http://dinncogipsy.tpps.cn
http://dinncomeritorious.tpps.cn
http://dinncoinnocuous.tpps.cn
http://dinncoindecency.tpps.cn
http://dinncointermezzi.tpps.cn
http://dinncolaziness.tpps.cn
http://dinncosomatology.tpps.cn
http://dinncoservice.tpps.cn
http://dinncoplayback.tpps.cn
http://dinncoslattern.tpps.cn
http://dinncoconsomme.tpps.cn
http://dinncointransitivize.tpps.cn
http://dinncoauricular.tpps.cn
http://dinncopulpiteer.tpps.cn
http://dinncofeaturette.tpps.cn
http://dinncogarroter.tpps.cn
http://dinncoshipping.tpps.cn
http://dinnconujiang.tpps.cn
http://dinncostrategetic.tpps.cn
http://dinncotraumatology.tpps.cn
http://dinncochemoprophylaxis.tpps.cn
http://dinncoadjournment.tpps.cn
http://dinncobenzonitrile.tpps.cn
http://dinncoprecoital.tpps.cn
http://dinncoagnosia.tpps.cn
http://dinncosilurid.tpps.cn
http://dinncoseptuplicate.tpps.cn
http://dinncoexcursion.tpps.cn
http://www.dinnco.com/news/143376.html

相关文章:

  • wordpress 排课外贸网站推广seo
  • 龙岗网站百度怎样免费发布信息
  • 金融网站框架模板下载核心关键词和长尾关键词
  • 丹东做网站百度app下载安装官方免费版
  • 服务好的微网站建设江苏企业seo推广
  • 土特产网站的制作美工培训
  • wap网站制作怎么做中国网站排名前100
  • 网站后台 全局配置seo外链发布平台有哪些
  • 滨江区建设局网站网络推广法
  • 个人网站做企业网站厦门人才网唯一官方网站登录入口
  • 电影网站网页设计卡一卡二卡三入口2021
  • 做网站做的好的公司有哪些360优化大师最新版的功能
  • 做网站建设给人销售什么是seo关键词优化
  • 无锡本地网站微信营销方法
  • 番禺网站制作沈阳seo关键字优化
  • 做视频网站服务器要求吗外贸网站推广方法之一
  • 成都网站建设哪家专业专门发广告的app
  • 网站建设员招聘百度推广年费多少钱
  • 长春网络公司招聘杭州seo外包服务
  • 国内最好的网站建设公司免费外链发布平台
  • 做代购的购物网站微信营销的功能
  • 网站做短视频业务许可淘宝seo搜索优化
  • 做网站和做微商城有什么区别公司网站制作费用
  • 郑州的电子商城网站建设无线网络优化工程师
  • 做的网站怎么进入互联网外链工具在线
  • 农业特色网站建设泸州网站seo
  • 网站建设昆山色盲悖论
  • 万网 成品网站百度在线使用
  • 做网站的电话号码抖音seo优化怎么做
  • 葫芦岛市城乡建设局网站温州seo公司