2026-01-19 23:45:08 +08:00
|
|
|
|
<!--
|
2026-02-01 23:42:12 +08:00
|
|
|
|
ApiConceptDemo.vue
|
|
|
|
|
|
目标:直观演示 API 的基本要素:地址 + 参数
|
2026-01-19 23:45:08 +08:00
|
|
|
|
-->
|
|
|
|
|
|
<template>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
<div class="demo">
|
2026-01-20 17:53:22 +08:00
|
|
|
|
<div class="header">
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<span class="icon">🔧</span>
|
|
|
|
|
|
<span class="title">互动演示:调用 API 需要什么?</span>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<div class="content">
|
|
|
|
|
|
<div class="step">
|
|
|
|
|
|
<div class="step-header">
|
|
|
|
|
|
<span class="step-num">1</span>
|
|
|
|
|
|
<span class="step-title">地址 (Endpoint) - 告诉服务器你要找谁</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="step-body">
|
|
|
|
|
|
<div class="url-bar">
|
|
|
|
|
|
<span class="url">https://api.example.com</span>
|
|
|
|
|
|
<input
|
|
|
|
|
|
v-model="endpoint"
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
class="endpoint-input"
|
|
|
|
|
|
placeholder="/users"
|
2026-02-18 17:38:10 +08:00
|
|
|
|
>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
</div>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
</div>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<div class="step">
|
|
|
|
|
|
<div class="step-header">
|
|
|
|
|
|
<span class="step-num">2</span>
|
|
|
|
|
|
<span class="step-title">参数 (Params) - 告诉服务器你要什么</span>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<div class="step-body">
|
|
|
|
|
|
<div class="params-box">
|
|
|
|
|
|
<div class="params-row">
|
|
|
|
|
|
<span class="param-label">页码:</span>
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<input
|
|
|
|
|
|
v-model.number="page"
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
class="param-input"
|
|
|
|
|
|
min="1"
|
|
|
|
|
|
>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div class="params-row">
|
|
|
|
|
|
<span class="param-label">每页数量:</span>
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<input
|
|
|
|
|
|
v-model.number="limit"
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
class="param-input small"
|
|
|
|
|
|
min="1"
|
|
|
|
|
|
max="100"
|
|
|
|
|
|
>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<button
|
|
|
|
|
|
class="send-btn"
|
|
|
|
|
|
:disabled="loading"
|
|
|
|
|
|
@click="sendRequest"
|
|
|
|
|
|
>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
{{ loading ? '发送中...' : '🚀 发送请求' }}
|
2026-01-20 17:53:22 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div
|
|
|
|
|
|
v-if="response"
|
|
|
|
|
|
class="response"
|
|
|
|
|
|
>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<div class="response-header">
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<span
|
|
|
|
|
|
class="status-badge"
|
|
|
|
|
|
:class="response.status >= 200 && response.status < 300 ? 'success' : 'error'"
|
|
|
|
|
|
>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
{{ response.status }} {{ response.statusText }}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span class="response-time">耗时: {{ response.time }}ms</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<pre class="response-body">{{ JSON.stringify(response.data, null, 2) }}</pre>
|
2026-01-20 17:53:22 +08:00
|
|
|
|
</div>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</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
|
|
|
|
const startTime = Date.now()
|
|
|
|
|
|
|
|
|
|
|
|
if (endpoint.value === '/users') {
|
|
|
|
|
|
// 限制最多返回3条,避免结果太长
|
|
|
|
|
|
const actualLimit = Math.min(limit.value, 3)
|
|
|
|
|
|
const users = []
|
|
|
|
|
|
for (let i = 1; i <= actualLimit; i++) {
|
|
|
|
|
|
users.push({
|
|
|
|
|
|
id: i,
|
|
|
|
|
|
name: `用户${(page.value - 1) * limit.value + i}`,
|
|
|
|
|
|
age: 20 + i
|
|
|
|
|
|
})
|
2026-01-20 17:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response.value = {
|
|
|
|
|
|
status: 200,
|
|
|
|
|
|
statusText: 'OK',
|
2026-02-01 23:42:12 +08:00
|
|
|
|
time: Date.now() - startTime,
|
|
|
|
|
|
data: {
|
|
|
|
|
|
users,
|
|
|
|
|
|
total: 100,
|
|
|
|
|
|
page: page.value,
|
|
|
|
|
|
limit: limit.value,
|
|
|
|
|
|
note: limit.value > 3 ? `仅显示前3条,共${limit.value}条` : null
|
|
|
|
|
|
}
|
2026-01-20 17:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
response.value = {
|
2026-02-01 23:42:12 +08:00
|
|
|
|
status: 404,
|
|
|
|
|
|
statusText: 'Not Found',
|
|
|
|
|
|
time: Date.now() - startTime,
|
|
|
|
|
|
data: { error: '找不到这个接口' }
|
2026-01-20 17:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-01 23:42:12 +08:00
|
|
|
|
|
|
|
|
|
|
loading.value = false
|
|
|
|
|
|
}, 300 + Math.random() * 200)
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.demo {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
border-radius: 12px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-01-20 17:53:22 +08:00
|
|
|
|
margin: 24px 0;
|
|
|
|
|
|
overflow: hidden;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.header {
|
2026-02-01 23:42:12 +08:00
|
|
|
|
padding: 14px 20px;
|
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;
|
|
|
|
|
|
gap: 10px;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.icon {
|
|
|
|
|
|
font-size: 20px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.title {
|
2026-01-20 17:53:22 +08:00
|
|
|
|
font-weight: 600;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
font-size: 15px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.content {
|
2026-01-20 17:53:22 +08:00
|
|
|
|
padding: 20px;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 16px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.step {
|
|
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
|
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;
|
|
|
|
|
|
gap: 10px;
|
|
|
|
|
|
padding: 10px 14px;
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
border-bottom: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.step-num {
|
|
|
|
|
|
width: 22px;
|
|
|
|
|
|
height: 22px;
|
|
|
|
|
|
border-radius: 50%;
|
|
|
|
|
|
background: var(--vp-c-brand);
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
|
font-weight: bold;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.step-title {
|
2026-01-20 17:53:22 +08:00
|
|
|
|
font-size: 13px;
|
|
|
|
|
|
font-weight: 600;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
color: var(--vp-c-text-1);
|
2026-01-20 17:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.step-body {
|
|
|
|
|
|
padding: 14px;
|
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;
|
|
|
|
|
|
gap: 8px;
|
|
|
|
|
|
background: #1e293b;
|
|
|
|
|
|
padding: 10px 12px;
|
|
|
|
|
|
border-radius: 6px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.url {
|
|
|
|
|
|
color: #94a3b8;
|
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.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-01-20 17:53:22 +08:00
|
|
|
|
font-size: 14px;
|
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-box {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 10px;
|
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-01 23:42:12 +08:00
|
|
|
|
gap: 10px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.param-label {
|
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
min-width: 70px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.param-input {
|
|
|
|
|
|
padding: 6px 10px;
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.param-input.small {
|
|
|
|
|
|
width: 60px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.send-btn {
|
2026-02-01 23:42:12 +08:00
|
|
|
|
padding: 12px;
|
|
|
|
|
|
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;
|
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
transition: opacity 0.2s;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:53:22 +08:00
|
|
|
|
.send-btn:disabled {
|
|
|
|
|
|
opacity: 0.7;
|
|
|
|
|
|
cursor: not-allowed;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.response {
|
|
|
|
|
|
margin-top: 8px;
|
|
|
|
|
|
animation: fadeIn 0.3s ease;
|
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-01 23:42:12 +08:00
|
|
|
|
justify-content: space-between;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
align-items: center;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
margin-bottom: 8px;
|
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-01 23:42:12 +08:00
|
|
|
|
font-size: 12px;
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.response-time {
|
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
|
color: var(--vp-c-text-3);
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
.response-body {
|
|
|
|
|
|
background: #1e293b;
|
|
|
|
|
|
color: #e2e8f0;
|
2026-01-20 08:51:04 +08:00
|
|
|
|
padding: 12px;
|
2026-02-01 23:42:12 +08:00
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
|
overflow-x: auto;
|
2026-02-14 20:23:34 +08:00
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
max-height: 200px;
|
|
|
|
|
|
margin: 0;
|
2026-01-20 17:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
@keyframes fadeIn {
|
|
|
|
|
|
from {
|
|
|
|
|
|
opacity: 0;
|
|
|
|
|
|
transform: translateY(5px);
|
|
|
|
|
|
}
|
|
|
|
|
|
to {
|
|
|
|
|
|
opacity: 1;
|
|
|
|
|
|
transform: translateY(0);
|
|
|
|
|
|
}
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
</style>
|