2026-01-19 23:45:08 +08:00
|
|
|
|
<!--
|
2026-01-20 08:51:04 +08:00
|
|
|
|
ApiQuickStartDemo.vue - 简化版
|
|
|
|
|
|
目标:用最简单的交互展示 API 调用流程
|
2026-01-19 23:45:08 +08:00
|
|
|
|
-->
|
|
|
|
|
|
<template>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
<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>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
<div class="error" v-else>
|
|
|
|
|
|
❌ 失败了:{{ result.error }}
|
2026-01-19 23:45:08 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
</div>
|
2026-01-19 23:45:08 +08:00
|
|
|
|
|
2026-01-20 08:51:04 +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>
|
2026-01-20 08:51:04 +08:00
|
|
|
|
import { ref } from 'vue'
|
2026-01-19 23:45:08 +08:00
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
const calling = ref(false)
|
2026-01-19 23:45:08 +08:00
|
|
|
|
const result = ref(null)
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
function callApi() {
|
|
|
|
|
|
calling.value = true
|
2026-01-19 23:45:08 +08:00
|
|
|
|
result.value = null
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
// 模拟 API 调用
|
|
|
|
|
|
setTimeout(() => {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
result.value = {
|
2026-01-20 08:51:04 +08:00
|
|
|
|
success: true,
|
|
|
|
|
|
data: 'Hello from API!'
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
2026-01-20 08:51:04 +08:00
|
|
|
|
calling.value = false
|
|
|
|
|
|
}, 800)
|
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;
|
|
|
|
|
|
padding: 20px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
margin: 16px 0;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.title {
|
2026-01-20 08:51:04 +08:00
|
|
|
|
font-size: 18px;
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
margin-bottom: 8px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.subtitle {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
margin-bottom: 16px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.box {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
padding: 20px;
|
|
|
|
|
|
text-align: center;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.call-btn {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
background: var(--vp-c-brand-1);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
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;
|
2026-01-20 08:51:04 +08:00
|
|
|
|
transition: all 0.2s;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.call-btn:hover:not(:disabled) {
|
|
|
|
|
|
opacity: 0.9;
|
|
|
|
|
|
transform: scale(1.05);
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.call-btn:disabled {
|
2026-01-19 23:45:08 +08:00
|
|
|
|
opacity: 0.6;
|
|
|
|
|
|
cursor: not-allowed;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.result {
|
|
|
|
|
|
margin-top: 16px;
|
|
|
|
|
|
padding: 12px;
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
font-size: 14px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.success {
|
|
|
|
|
|
background: #dcfce7;
|
|
|
|
|
|
color: #166534;
|
|
|
|
|
|
border: 1px solid #86efac;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.error {
|
|
|
|
|
|
background: #fee2e2;
|
|
|
|
|
|
color: #991b1b;
|
|
|
|
|
|
border: 1px solid #fca5a5;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 08:51:04 +08:00
|
|
|
|
.explain {
|
|
|
|
|
|
margin-top: 16px;
|
|
|
|
|
|
padding: 12px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
2026-01-20 08:51:04 +08:00
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
font-size: 14px;
|
2026-01-19 23:45:08 +08:00
|
|
|
|
line-height: 1.6;
|
2026-01-20 08:51:04 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
2026-01-19 23:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
</style>
|