-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from XPoet/dev
Dev
- Loading branch information
Showing
9 changed files
with
217 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { MockMethod } from 'vite-plugin-mock' | ||
|
||
const mockArr = [ | ||
{ | ||
url: '/api/get', | ||
method: 'get', | ||
response: (res: { query: any }) => { | ||
return { | ||
code: 0, | ||
data: { | ||
name: res.query | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
url: '/api/post', | ||
method: 'post', | ||
timeout: 2000, | ||
response: { | ||
code: 0, | ||
data: { | ||
name: 'vben' | ||
} | ||
} | ||
}, | ||
{ | ||
url: '/api/test', | ||
method: 'get', | ||
response: (res: { body: any }) => { | ||
console.log('mock funciton: /api/test/ ----', res) | ||
return { | ||
code: 0, | ||
message: 'ok', | ||
data: { | ||
name: 'Mock' | ||
} | ||
} | ||
} | ||
} | ||
] | ||
|
||
export default mockArr as MockMethod[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer' | ||
|
||
// import your mock.ts | ||
// you can import only one file if you use vite.mock.config.ts | ||
// aslo can import.meta.glob to import all files | ||
import testModule from '../mock/test' | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export function setupProdMockServer() { | ||
createProdMockServer([...testModule]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<template> | ||
<div class="mock-container page-container"> | ||
<div class="page-title">Axios Test Page</div> | ||
<div class="user-info-container"> | ||
<el-card class="box-card"> | ||
<template #header> | ||
<div class="card-header"> | ||
<span>Mock</span> | ||
<el-button class="button" type="text" @click="getTestInfo" | ||
>Click to get mock | ||
</el-button> | ||
</div> | ||
</template> | ||
<div class="info-list-box" v-loading="loading"> | ||
<div class="text item" v-if="mockInfo?.code">code: {{ mockInfo?.code }}</div> | ||
<div class="text item" v-if="mockInfo?.data">data: {{ mockInfo?.data }}</div> | ||
<div class="text item" v-if="mockInfo?.message"> | ||
msg: {{ mockInfo?.message }} | ||
</div> | ||
</div> | ||
</el-card> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, ref, Ref } from 'vue' | ||
import axios from '../utils/axios' | ||
export default defineComponent({ | ||
name: 'Mock', | ||
setup() { | ||
const mockInfo: Ref = ref(null) | ||
const loading = ref(false) | ||
const getTestInfo = () => { | ||
loading.value = true | ||
axios({ | ||
method: 'get', | ||
url: '/api/test', | ||
isMock: true | ||
}) | ||
.then((response) => { | ||
mockInfo.value = response.data | ||
loading.value = false | ||
}) | ||
.catch((error) => { | ||
loading.value = false | ||
console.error(error) | ||
}) | ||
} | ||
return { | ||
mockInfo, | ||
loading, | ||
getTestInfo | ||
} | ||
} | ||
}) | ||
</script> | ||
|
||
<style scoped lang="stylus"> | ||
.axios-container { | ||
.user-info-container { | ||
display flex | ||
justify-content center | ||
width 100% | ||
.info-list-box { | ||
padding 10px | ||
.text { | ||
font-size: 14px; | ||
} | ||
.item { | ||
margin-bottom: 18px; | ||
} | ||
} | ||
.card-header { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
} | ||
.box-card { | ||
width: 480px; | ||
} | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
import { defineConfig } from 'vite' | ||
import { UserConfigExport, ConfigEnv } from 'vite' | ||
import vue from '@vitejs/plugin-vue' | ||
import { viteMockServe } from 'vite-plugin-mock' | ||
// 如果编辑器提示 path 模块找不到,则可以安装一下 @types/node -> npm i @types/node -D | ||
import { resolve } from 'path' | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [vue()], | ||
resolve: { | ||
alias: { | ||
'@': resolve(__dirname, 'src') // 设置 `@` 指向 `src` 目录 | ||
} | ||
}, | ||
base: './', // 设置打包路径 | ||
server: { | ||
port: 4500, // 设置服务启动端口号 | ||
open: true, // 设置服务启动时是否自动打开浏览器 | ||
cors: true // 允许跨域 | ||
export default ({ command }: ConfigEnv): UserConfigExport => { | ||
const prodMock = true | ||
return { | ||
plugins: [ | ||
vue(), | ||
viteMockServe({ | ||
mockPath: 'mock', | ||
logger: true, | ||
localEnabled: command === 'serve', | ||
prodEnabled: command !== 'serve' && prodMock, | ||
// 这样可以控制关闭mock的时候不让mock打包到最终代码内 | ||
injectCode: ` | ||
import { setupProdMockServer } from './mockProdServer'; | ||
setupProdMockServer(); | ||
` | ||
}) | ||
], | ||
resolve: { | ||
alias: { | ||
'@': resolve(__dirname, 'src') // 设置 `@` 指向 `src` 目录 | ||
} | ||
}, | ||
base: './', // 设置打包路径 | ||
server: { | ||
port: 4500, // 设置服务启动端口号 | ||
open: true, // 设置服务启动时是否自动打开浏览器 | ||
cors: true // 允许跨域 | ||
|
||
// 设置代理,根据我们项目实际情况配置 | ||
// proxy: { | ||
// '/api': { | ||
// target: 'http://xxx.xxx.xxx.xxx:x000', | ||
// changeOrigin: true, | ||
// secure: false, | ||
// rewrite: (path) => path.replace('/api/', '/') | ||
// } | ||
// }, | ||
// 设置代理,根据我们项目实际情况配置 | ||
// proxy: { | ||
// '/api': { | ||
// target: 'http://xxx.xxx.xxx.xxx:x000', | ||
// changeOrigin: true, | ||
// secure: false, | ||
// rewrite: (path) => path.replace('/api/', '/') | ||
// } | ||
// }, | ||
} | ||
} | ||
}) | ||
} |