2026-01-19 23:45:08 +08:00
|
|
|
|
<!--
|
2026-02-23 01:50:43 +08:00
|
|
|
|
ApiConceptDemo.vue - 紧凑版
|
2026-02-01 23:42:12 +08:00
|
|
|
|
目标:直观演示 API 的基本要素:地址 + 参数
|
2026-01-19 23:45:08 +08:00
|
|
|
|
-->
|
|
|
|
|
|
<template>
|
2026-02-23 01:50:43 +08:00
|
|
|
|
<div class="demo-root">
|
|
|
|
|
|
<div class="demo-header">
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<span class="icon">🔧</span>
|
2026-02-23 01:50:43 +08:00
|
|
|
|
<span class="title">调用 API 需要什么?</span>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-23 01:50:43 +08:00
|
|
|
|
<div class="demo-layout">
|
|
|
|
|
|
<div class="left-panel">
|
|
|
|
|
|
<div class="step">
|
|
|
|
|
|
<div class="step-header">
|
|
|
|
|
|
<span class="step-num">1</span>
|
|
|
|
|
|
<span class="step-title">地址 (Endpoint)</span>
|
|
|
|
|
|
</div>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<div class="url-bar">
|
2026-02-23 01:50:43 +08:00
|
|
|
|
<span class="url-base">https://api.example.com</span>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<input
|
|
|
|
|
|
v-model="endpoint"
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
class="endpoint-input"
|
|
|
|
|
|
placeholder="/users"
|
2026-02-23 01:50:43 +08:00
|
|
|
|
/>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
</div>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-23 01:50:43 +08:00
|
|
|
|
<div class="step">
|
|
|
|
|
|
<div class="step-header">
|
|
|
|
|
|
<span class="step-num">2</span>
|
|
|
|
|
|
<span class="step-title">参数 (Params)</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="params-row">
|
|
|
|
|
|
<label>页码:</label>
|
|
|
|
|
|
<input
|
|
|
|
|
|
v-model.number="page"
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
class="param-input"
|
|
|
|
|
|
min="1"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<label>每页:</label>
|
|
|
|
|
|
<input
|
|
|
|
|
|
v-model.number="limit"
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
class="param-input"
|
|
|
|
|
|
min="1"
|
|
|
|
|
|
max="100"
|
|
|
|
|
|
/>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
</div>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-23 01:50:43 +08:00
|
|
|
|
<button class="send-btn" :disabled="loading" @click="sendRequest">
|
|
|
|
|
|
{{ loading ? '发送中...' : '🚀 发送请求' }}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
|
2026-02-23 01:50:43 +08:00
|
|
|
|
<div class="right-panel">
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<div class="response-header">
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<span
|
2026-02-23 01:50:43 +08:00
|
|
|
|
v-if="response"
|
2026-02-18 17:38:10 +08:00
|
|
|
|
class="status-badge"
|
2026-02-23 01:50:43 +08:00
|
|
|
|
:class="
|
|
|
|
|
|
response.status >= 200 && response.status < 300
|
|
|
|
|
|
? 'success'
|
|
|
|
|
|
: 'error'
|
|
|
|
|
|
"
|
2026-02-18 17:38:10 +08:00
|
|
|
|
>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
{{ response.status }} {{ response.statusText }}
|
|
|
|
|
|
</span>
|
2026-02-23 01:50:43 +08:00
|
|
|
|
<span v-else class="status-badge pending">等待请求</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div v-if="response" class="response-body">
|
|
|
|
|
|
<pre>{{ JSON.stringify(response.data, null, 2) }}</pre>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
</div>
|
2026-02-23 01:50:43 +08:00
|
|
|
|
<div v-else class="response-empty">点击发送按钮查看结果</div>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
</div>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
2026-02-23 01:50:43 +08:00
|
|
|
|
|
|
|
|
|
|
<div class="info-box">
|
|
|
|
|
|
<strong>核心思想:</strong>
|
2026-02-23 12:09:47 +08:00
|
|
|
|
<span>无论哪种 API,结构都一样:地址(找谁)+ 参数(要什么)=
|
|
|
|
|
|
响应(得到什么)。</span>
|
2026-02-23 01:50:43 +08:00
|
|
|
|
</div>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
import { ref } from 'vue'
|
2026-01-20 17:53:22 +08:00
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
const endpoint = ref('/users')
|
|
|
|
|
|
const page = ref(1)
|
|
|
|
|
|
const limit = ref(5)
|
2026-01-20 17:53:22 +08:00
|
|
|
|
const loading = ref(false)
|
|
|
|
|
|
const response = ref(null)
|
|
|
|
|
|
|
|
|
|
|
|
function sendRequest() {
|
|
|
|
|
|
loading.value = true
|
|
|
|
|
|
response.value = null
|
|
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2026-02-01 23:42:12 +08:00
|
|
|
|
if (endpoint.value === '/users') {
|
|
|
|
|
|
const actualLimit = Math.min(limit.value, 3)
|
|
|
|
|
|
const users = []
|
|
|
|
|
|
for (let i = 1; i <= actualLimit; i++) {
|
|
|
|
|
|
users.push({
|
|
|
|
|
|
id: i,
|
2026-02-23 01:50:43 +08:00
|
|
|
|
name: `用户${(page.value - 1) * limit.value + i}`
|
2026-02-01 23:42:12 +08:00
|
|
|
|
})
|
2026-01-20 17:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
response.value = {
|
|
|
|
|
|
status: 200,
|
|
|
|
|
|
statusText: 'OK',
|
2026-02-23 01:50:43 +08:00
|
|
|
|
data: { users, total: 100, page: page.value }
|
2026-01-20 17:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
response.value = {
|
2026-02-01 23:42:12 +08:00
|
|
|
|
status: 404,
|
|
|
|
|
|
statusText: 'Not Found',
|
|
|
|
|
|
data: { error: '找不到这个接口' }
|
2026-01-20 17:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-01 23:42:12 +08:00
|
|
|
|
loading.value = false
|
2026-02-23 01:50:43 +08:00
|
|
|
|
}, 300)
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
2026-02-23 01:50:43 +08:00
|
|
|
|
.demo-root {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-02-23 01:50:43 +08:00
|
|
|
|
border-radius: 10px;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
overflow: hidden;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
margin: 1rem 0;
|
|
|
|
|
|
font-size: 0.85rem;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:50:43 +08:00
|
|
|
|
.demo-header {
|
|
|
|
|
|
padding: 10px 16px;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
border-bottom: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
gap: 8px;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.icon {
|
2026-02-23 01:50:43 +08:00
|
|
|
|
font-size: 18px;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
.title {
|
2026-01-20 17:53:22 +08:00
|
|
|
|
font-weight: 600;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
font-size: 0.9rem;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:50:43 +08:00
|
|
|
|
.demo-layout {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.left-panel {
|
|
|
|
|
|
flex: 1;
|
|
|
|
|
|
padding: 12px;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 10px;
|
|
|
|
|
|
border-right: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.right-panel {
|
|
|
|
|
|
width: 220px;
|
|
|
|
|
|
padding: 12px;
|
|
|
|
|
|
background: var(--vp-c-bg);
|
2026-01-20 17:53:22 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
gap: 8px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@media (max-width: 640px) {
|
|
|
|
|
|
.demo-layout {
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
}
|
|
|
|
|
|
.left-panel {
|
|
|
|
|
|
border-right: none;
|
|
|
|
|
|
border-bottom: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
}
|
|
|
|
|
|
.right-panel {
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
}
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.step {
|
|
|
|
|
|
background: var(--vp-c-bg);
|
2026-02-23 01:50:43 +08:00
|
|
|
|
border-radius: 6px;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
overflow: hidden;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.step-header {
|
2026-01-20 17:53:22 +08:00
|
|
|
|
display: flex;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
align-items: center;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
gap: 8px;
|
|
|
|
|
|
padding: 6px 10px;
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
2026-02-01 23:42:12 +08:00
|
|
|
|
border-bottom: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.step-num {
|
2026-02-23 01:50:43 +08:00
|
|
|
|
width: 18px;
|
|
|
|
|
|
height: 18px;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
border-radius: 50%;
|
|
|
|
|
|
background: var(--vp-c-brand);
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
justify-content: center;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
font-size: 0.7rem;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
font-weight: bold;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.step-title {
|
2026-02-23 01:50:43 +08:00
|
|
|
|
font-size: 0.8rem;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
font-weight: 600;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.url-bar {
|
2026-01-20 17:53:22 +08:00
|
|
|
|
display: flex;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
align-items: center;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
gap: 4px;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
background: #1e293b;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
padding: 8px 10px;
|
|
|
|
|
|
border-radius: 0 0 6px 6px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:50:43 +08:00
|
|
|
|
.url-base {
|
2026-02-01 23:42:12 +08:00
|
|
|
|
color: #94a3b8;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
font-size: 0.7rem;
|
|
|
|
|
|
white-space: nowrap;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.endpoint-input {
|
|
|
|
|
|
flex: 1;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
background: transparent;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
border: none;
|
|
|
|
|
|
color: #60a5fa;
|
|
|
|
|
|
font-family: monospace;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
font-size: 0.8rem;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
outline: none;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.params-row {
|
2026-01-20 17:53:22 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
gap: 8px;
|
|
|
|
|
|
padding: 8px 10px;
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
border-radius: 0 0 6px 6px;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:50:43 +08:00
|
|
|
|
.params-row label {
|
|
|
|
|
|
font-size: 0.75rem;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.param-input {
|
2026-02-23 01:50:43 +08:00
|
|
|
|
width: 50px;
|
|
|
|
|
|
padding: 4px 6px;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-02-23 01:50:43 +08:00
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
font-size: 0.8rem;
|
|
|
|
|
|
background: var(--vp-c-bg);
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.send-btn {
|
2026-02-23 01:50:43 +08:00
|
|
|
|
padding: 10px;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
background: var(--vp-c-brand);
|
2026-01-20 17:53:22 +08:00
|
|
|
|
color: white;
|
|
|
|
|
|
border: none;
|
2026-02-14 20:23:34 +08:00
|
|
|
|
border-radius: 6px;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
font-weight: 600;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
font-size: 0.85rem;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
cursor: pointer;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.send-btn:disabled {
|
2026-02-23 01:50:43 +08:00
|
|
|
|
opacity: 0.6;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
cursor: not-allowed;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.response-header {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
display: flex;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
justify-content: center;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.status-badge {
|
|
|
|
|
|
padding: 4px 10px;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
border-radius: 4px;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
font-size: 0.75rem;
|
2026-01-20 08:51:04 +08:00
|
|
|
|
font-weight: bold;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.status-badge.success {
|
|
|
|
|
|
background: #dcfce7;
|
|
|
|
|
|
color: #166534;
|
|
|
|
|
|
}
|
2026-01-19 23:45:08 +08:00
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.status-badge.error {
|
|
|
|
|
|
background: #fee2e2;
|
|
|
|
|
|
color: #991b1b;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:50:43 +08:00
|
|
|
|
.status-badge.pending {
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-02-01 23:42:12 +08:00
|
|
|
|
color: var(--vp-c-text-3);
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.response-body {
|
2026-02-23 01:50:43 +08:00
|
|
|
|
flex: 1;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
background: #1e293b;
|
|
|
|
|
|
border-radius: 6px;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
padding: 8px;
|
|
|
|
|
|
overflow: auto;
|
|
|
|
|
|
max-height: 120px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.response-body pre {
|
2026-02-01 23:42:12 +08:00
|
|
|
|
margin: 0;
|
2026-02-23 01:50:43 +08:00
|
|
|
|
font-family: monospace;
|
|
|
|
|
|
font-size: 0.7rem;
|
|
|
|
|
|
color: #e2e8f0;
|
|
|
|
|
|
white-space: pre-wrap;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:50:43 +08:00
|
|
|
|
.response-empty {
|
|
|
|
|
|
flex: 1;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
color: var(--vp-c-text-3);
|
|
|
|
|
|
font-size: 0.8rem;
|
|
|
|
|
|
font-style: italic;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.info-box {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
gap: 0.25rem;
|
|
|
|
|
|
padding: 10px 14px;
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
border-top: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
font-size: 0.8rem;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.info-box strong {
|
|
|
|
|
|
white-space: nowrap;
|
|
|
|
|
|
flex-shrink: 0;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
</style>
|