2026-01-18 10:24:35 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="imperative-declarative-demo">
|
2026-02-14 12:14:07 +08:00
|
|
|
|
<div class="demo-header">
|
|
|
|
|
|
<span class="icon">🔄</span>
|
|
|
|
|
|
<span class="title">命令式 vs 声明式</span>
|
|
|
|
|
|
<span class="subtitle">两种编程思维的对比(通俗说:手动操作 vs 自动响应)</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="demo-content">
|
|
|
|
|
|
<div class="demo-grid">
|
|
|
|
|
|
<!-- Imperative (jQuery Style) -->
|
|
|
|
|
|
<div class="panel imperative">
|
|
|
|
|
|
<div class="panel-header">
|
|
|
|
|
|
<span class="badge yellow">命令式 (Imperative)</span>
|
|
|
|
|
|
<span class="sub-text">jQuery Style - 手动操作</span>
|
2026-01-18 10:24:35 +08:00
|
|
|
|
</div>
|
2026-02-14 12:14:07 +08:00
|
|
|
|
<div class="code-preview">
|
|
|
|
|
|
<code>
|
2026-02-18 17:38:10 +08:00
|
|
|
|
// 手动操作 DOM<br>
|
|
|
|
|
|
$('#count').text(val);<br>
|
2026-02-14 12:14:07 +08:00
|
|
|
|
if (val > 5) $('#msg').show();
|
|
|
|
|
|
</code>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="interactive-area">
|
|
|
|
|
|
<div class="output-box">
|
|
|
|
|
|
Count: <span id="imp-count-display">{{ impCount }}</span>
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div
|
|
|
|
|
|
v-show="impShowMsg"
|
|
|
|
|
|
class="warning-msg"
|
|
|
|
|
|
>
|
|
|
|
|
|
⚠️ Count is high!
|
|
|
|
|
|
</div>
|
2026-02-14 12:14:07 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div class="actions">
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<button
|
|
|
|
|
|
class="btn"
|
|
|
|
|
|
@click="impIncrement"
|
|
|
|
|
|
>
|
|
|
|
|
|
Step 1: Value++
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
class="btn"
|
|
|
|
|
|
:disabled="!impChanged"
|
|
|
|
|
|
@click="impUpdateText"
|
|
|
|
|
|
>
|
2026-02-14 12:14:07 +08:00
|
|
|
|
Step 2: Update Text
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
class="btn"
|
|
|
|
|
|
:disabled="!impTextUpdated"
|
2026-02-18 17:38:10 +08:00
|
|
|
|
@click="impCheckState"
|
2026-02-14 12:14:07 +08:00
|
|
|
|
>
|
|
|
|
|
|
Step 3: Check Logic
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div class="status-log">
|
|
|
|
|
|
{{ impStatus }}
|
|
|
|
|
|
</div>
|
2026-01-18 10:24:35 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-14 12:14:07 +08:00
|
|
|
|
<!-- Declarative (Vue Style) -->
|
|
|
|
|
|
<div class="panel declarative">
|
|
|
|
|
|
<div class="panel-header">
|
|
|
|
|
|
<span class="badge green">声明式 (Declarative)</span>
|
|
|
|
|
|
<span class="sub-text">Vue/React Style - 自动响应</span>
|
2026-01-18 10:24:35 +08:00
|
|
|
|
</div>
|
2026-02-14 12:14:07 +08:00
|
|
|
|
<div class="code-preview">
|
|
|
|
|
|
<code v-pre>
|
|
|
|
|
|
// 只需要绑定数据
|
|
|
|
|
|
{{ count }}
|
|
|
|
|
|
<div v-if="count > 5">...</div>
|
|
|
|
|
|
</code>
|
2026-01-18 12:21:49 +08:00
|
|
|
|
</div>
|
2026-02-14 12:14:07 +08:00
|
|
|
|
<div class="interactive-area">
|
|
|
|
|
|
<div class="output-box">
|
|
|
|
|
|
Count: <span>{{ decCount }}</span>
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div
|
|
|
|
|
|
v-if="decCount > 5"
|
|
|
|
|
|
class="warning-msg"
|
|
|
|
|
|
>
|
|
|
|
|
|
⚠️ Count is high!
|
|
|
|
|
|
</div>
|
2026-02-14 12:14:07 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div class="actions">
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<button
|
|
|
|
|
|
class="btn primary"
|
|
|
|
|
|
@click="decIncrement"
|
|
|
|
|
|
>
|
2026-02-14 12:14:07 +08:00
|
|
|
|
Value++ (Auto Render)
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="status-log">
|
|
|
|
|
|
Framework handles DOM updates automatically.
|
|
|
|
|
|
</div>
|
2026-01-18 10:24:35 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-02-14 12:14:07 +08:00
|
|
|
|
|
|
|
|
|
|
<div class="info-box">
|
|
|
|
|
|
<span class="icon">💡</span>
|
|
|
|
|
|
<strong>核心思想:</strong>命令式像"手把手教电脑怎么做",声明式像"告诉电脑要什么,它自己搞定"。
|
|
|
|
|
|
</div>
|
2026-01-18 10:24:35 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
|
import { ref } from 'vue'
|
|
|
|
|
|
|
|
|
|
|
|
// Imperative State
|
|
|
|
|
|
const impCount = ref(0)
|
|
|
|
|
|
const impShowMsg = ref(false)
|
|
|
|
|
|
const impChanged = ref(false)
|
|
|
|
|
|
const impTextUpdated = ref(false)
|
|
|
|
|
|
const impStatus = ref('Ready.')
|
|
|
|
|
|
|
|
|
|
|
|
const impIncrement = () => {
|
|
|
|
|
|
// Logic layer changes, but DOM doesn't
|
2026-01-18 12:21:49 +08:00
|
|
|
|
impStatus.value =
|
|
|
|
|
|
'Variable `count` is now ' + (impCount.value + 1) + '. DOM is NOT updated.'
|
2026-01-18 10:24:35 +08:00
|
|
|
|
impCount.value++
|
|
|
|
|
|
impChanged.value = true
|
|
|
|
|
|
impTextUpdated.value = false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const impUpdateText = () => {
|
|
|
|
|
|
// Manually update text
|
|
|
|
|
|
impStatus.value = 'DOM text updated manually.'
|
|
|
|
|
|
impChanged.value = false
|
|
|
|
|
|
impTextUpdated.value = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const impCheckState = () => {
|
|
|
|
|
|
// Manually check logic
|
|
|
|
|
|
if (impCount.value > 5) {
|
|
|
|
|
|
impShowMsg.value = true
|
|
|
|
|
|
impStatus.value = 'Logic checked: > 5. Manually showing message.'
|
|
|
|
|
|
} else {
|
|
|
|
|
|
impShowMsg.value = false
|
|
|
|
|
|
impStatus.value = 'Logic checked: <= 5. No message.'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Declarative State
|
|
|
|
|
|
const decCount = ref(0)
|
|
|
|
|
|
const decIncrement = () => {
|
|
|
|
|
|
decCount.value++
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.imperative-declarative-demo {
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-02-14 20:23:34 +08:00
|
|
|
|
border-radius: 6px;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-02-14 20:23:34 +08:00
|
|
|
|
padding: 0.75rem;
|
|
|
|
|
|
margin: 0.5rem 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-02-14 12:14:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-header {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
|
margin-bottom: 0.75rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-header .icon {
|
|
|
|
|
|
font-size: 1.25rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-header .title {
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
font-size: 1rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-header .subtitle {
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
font-size: 0.85rem;
|
|
|
|
|
|
margin-left: 0.5rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-content {
|
|
|
|
|
|
margin-bottom: 0.5rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-grid {
|
|
|
|
|
|
display: grid;
|
|
|
|
|
|
grid-template-columns: 1fr 1fr;
|
2026-02-14 12:14:07 +08:00
|
|
|
|
gap: 1rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@media (max-width: 640px) {
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.demo-grid {
|
|
|
|
|
|
grid-template-columns: 1fr;
|
|
|
|
|
|
}
|
2026-01-18 10:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.panel {
|
|
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-02-14 20:23:34 +08:00
|
|
|
|
border-radius: 6px;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
overflow: hidden;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.panel-header {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
padding: 0.75rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
border-bottom: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.badge {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
font-size: 0.75rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
font-weight: bold;
|
2026-02-14 12:14:07 +08:00
|
|
|
|
padding: 2px 8px;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
}
|
2026-02-14 12:14:07 +08:00
|
|
|
|
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.badge.yellow {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
background: var(--vp-c-warning);
|
2026-01-18 12:21:49 +08:00
|
|
|
|
}
|
2026-02-14 12:14:07 +08:00
|
|
|
|
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.badge.green {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
background: var(--vp-c-success);
|
2026-01-18 12:21:49 +08:00
|
|
|
|
}
|
2026-01-18 10:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
.sub-text {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
font-size: 0.75rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.code-preview {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
padding: 0.75rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
font-family: monospace;
|
2026-02-14 12:14:07 +08:00
|
|
|
|
font-size: 0.75rem;
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
height: 70px;
|
|
|
|
|
|
overflow: hidden;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.interactive-area {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
padding: 0.75rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
flex: 1;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
2026-02-14 12:14:07 +08:00
|
|
|
|
gap: 0.75rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.output-box {
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-02-14 12:14:07 +08:00
|
|
|
|
padding: 0.75rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
text-align: center;
|
|
|
|
|
|
font-weight: bold;
|
2026-02-14 12:14:07 +08:00
|
|
|
|
min-height: 70px;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.warning-msg {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
color: var(--vp-c-danger);
|
2026-01-18 10:24:35 +08:00
|
|
|
|
margin-top: 0.5rem;
|
2026-02-14 12:14:07 +08:00
|
|
|
|
font-size: 0.85rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.actions {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.btn {
|
|
|
|
|
|
padding: 0.5rem;
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 4px;
|
2026-02-14 12:14:07 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
2026-01-18 10:24:35 +08:00
|
|
|
|
cursor: pointer;
|
2026-02-14 12:14:07 +08:00
|
|
|
|
font-size: 0.75rem;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
transition: all 0.2s;
|
|
|
|
|
|
}
|
2026-02-14 12:14:07 +08:00
|
|
|
|
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.btn:hover:not(:disabled) {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-01-18 12:21:49 +08:00
|
|
|
|
}
|
2026-02-14 12:14:07 +08:00
|
|
|
|
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.btn:disabled {
|
|
|
|
|
|
opacity: 0.5;
|
|
|
|
|
|
cursor: not-allowed;
|
|
|
|
|
|
}
|
2026-02-14 12:14:07 +08:00
|
|
|
|
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.btn.primary {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
background: var(--vp-c-brand);
|
2026-01-18 12:21:49 +08:00
|
|
|
|
color: white;
|
|
|
|
|
|
border: none;
|
|
|
|
|
|
}
|
2026-02-14 12:14:07 +08:00
|
|
|
|
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.btn.primary:hover {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
opacity: 0.9;
|
2026-01-18 12:21:49 +08:00
|
|
|
|
}
|
2026-01-18 10:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
.status-log {
|
2026-02-14 12:14:07 +08:00
|
|
|
|
font-size: 0.7rem;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
2026-01-18 10:24:35 +08:00
|
|
|
|
font-style: italic;
|
|
|
|
|
|
min-height: 1.2em;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-14 12:14:07 +08:00
|
|
|
|
.info-box {
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
padding: 0.75rem;
|
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
font-size: 0.85rem;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
gap: 0.25rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.info-box .icon {
|
|
|
|
|
|
flex-shrink: 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.info-box strong {
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
2026-01-18 10:24:35 +08:00
|
|
|
|
}
|
2026-01-18 12:21:49 +08:00
|
|
|
|
</style>
|