Files
test-repo/docs/.vitepress/theme/components/appendix/api-intro/ApiQuickStartDemo.vue
T

133 lines
2.5 KiB
Vue
Raw Normal View History

2026-01-19 23:45:08 +08:00
<!--
ApiQuickStartDemo.vue - 简化版
目标用最简单的交互展示 API 调用流程
2026-01-19 23:45:08 +08:00
-->
<template>
<div class="demo">
<div class="title">🎮 试试看调用一次 API</div>
<p class="subtitle">点一下按钮看看会发生什么</p>
<div class="box">
<button class="call-btn" :disabled="calling" @click="callApi">
{{ calling ? '调用中...' : '🔘 点我调用 API' }}
</button>
<div class="result" v-if="result">
<div class="success" v-if="result.success">
成功API 返回了{{ result.data }}
2026-01-19 23:45:08 +08:00
</div>
<div class="error" v-else>
失败了{{ result.error }}
2026-01-19 23:45:08 +08:00
</div>
</div>
</div>
2026-01-19 23:45:08 +08:00
<div class="explain">
<p><strong>你看</strong>你只需要点一下按钮调用 API就会得到结果</p>
<p>这就是 API 的本质<strong>按约定把请求交给对方对方按约定把结果给你</strong></p>
2026-01-19 23:45:08 +08:00
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
2026-01-19 23:45:08 +08:00
const calling = ref(false)
2026-01-19 23:45:08 +08:00
const result = ref(null)
function callApi() {
calling.value = true
2026-01-19 23:45:08 +08:00
result.value = null
// 模拟 API 调用
setTimeout(() => {
2026-01-19 23:45:08 +08:00
result.value = {
success: true,
data: 'Hello from API!'
2026-01-19 23:45:08 +08:00
}
calling.value = false
}, 800)
2026-01-19 23:45:08 +08:00
}
</script>
<style scoped>
.demo {
2026-01-19 23:45:08 +08:00
border: 1px solid var(--vp-c-divider);
border-radius: 12px;
padding: 20px;
2026-01-19 23:45:08 +08:00
background: var(--vp-c-bg-soft);
margin: 16px 0;
2026-01-19 23:45:08 +08:00
}
.title {
font-size: 18px;
font-weight: bold;
margin-bottom: 8px;
2026-01-19 23:45:08 +08:00
color: var(--vp-c-text-1);
}
.subtitle {
2026-01-19 23:45:08 +08:00
color: var(--vp-c-text-2);
margin-bottom: 16px;
2026-01-19 23:45:08 +08:00
}
.box {
2026-01-19 23:45:08 +08:00
background: var(--vp-c-bg);
border: 1px solid var(--vp-c-divider);
border-radius: 8px;
padding: 20px;
text-align: center;
2026-01-19 23:45:08 +08:00
}
.call-btn {
2026-01-19 23:45:08 +08:00
background: var(--vp-c-brand-1);
color: white;
border: none;
padding: 12px 24px;
font-size: 16px;
font-weight: bold;
border-radius: 8px;
2026-01-19 23:45:08 +08:00
cursor: pointer;
transition: all 0.2s;
2026-01-19 23:45:08 +08:00
}
.call-btn:hover:not(:disabled) {
opacity: 0.9;
transform: scale(1.05);
2026-01-19 23:45:08 +08:00
}
.call-btn:disabled {
2026-01-19 23:45:08 +08:00
opacity: 0.6;
cursor: not-allowed;
}
.result {
margin-top: 16px;
padding: 12px;
border-radius: 8px;
font-size: 14px;
2026-01-19 23:45:08 +08:00
}
.success {
background: #dcfce7;
color: #166534;
border: 1px solid #86efac;
2026-01-19 23:45:08 +08:00
}
.error {
background: #fee2e2;
color: #991b1b;
border: 1px solid #fca5a5;
2026-01-19 23:45:08 +08:00
}
.explain {
margin-top: 16px;
padding: 12px;
2026-01-19 23:45:08 +08:00
background: var(--vp-c-bg);
border-radius: 8px;
font-size: 14px;
2026-01-19 23:45:08 +08:00
line-height: 1.6;
color: var(--vp-c-text-2);
2026-01-19 23:45:08 +08:00
}
</style>