2026-02-15 18:15:42 +08:00
|
|
|
<script setup>
|
|
|
|
|
import { ref } from 'vue'
|
|
|
|
|
|
|
|
|
|
const name = ref('张三')
|
|
|
|
|
const age = ref(25)
|
|
|
|
|
const isStudent = ref(true)
|
|
|
|
|
const showMessage = ref('')
|
2026-02-17 01:39:59 +08:00
|
|
|
const messageType = ref('')
|
|
|
|
|
let messageTimer = null
|
|
|
|
|
|
|
|
|
|
const clearMessage = () => {
|
|
|
|
|
showMessage.value = ''
|
|
|
|
|
messageType.value = ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const setMessage = (msg, type) => {
|
|
|
|
|
if (messageTimer) clearTimeout(messageTimer)
|
|
|
|
|
showMessage.value = msg
|
|
|
|
|
messageType.value = type
|
|
|
|
|
messageTimer = setTimeout(() => clearMessage(), 2000)
|
|
|
|
|
}
|
2026-02-15 18:15:42 +08:00
|
|
|
|
|
|
|
|
const modifyAge = () => {
|
|
|
|
|
age.value = 26
|
2026-02-17 01:39:59 +08:00
|
|
|
setMessage('✅ let 可以修改', 'success')
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const modifyName = () => {
|
2026-02-17 01:39:59 +08:00
|
|
|
setMessage('❌ const 不能改', 'error')
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const reset = () => {
|
|
|
|
|
name.value = '张三'
|
|
|
|
|
age.value = 25
|
|
|
|
|
isStudent.value = true
|
2026-02-17 01:39:59 +08:00
|
|
|
clearMessage()
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<div class="variable-box-demo">
|
2026-02-17 01:39:59 +08:00
|
|
|
<div class="demo-header">
|
|
|
|
|
<span class="title">📦 变量就像带名字的盒子</span>
|
|
|
|
|
</div>
|
2026-02-15 18:15:42 +08:00
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
<div class="boxes-row">
|
|
|
|
|
<div class="var-box" :class="{ error: messageType === 'error' }">
|
|
|
|
|
<div class="box-tag const">const</div>
|
|
|
|
|
<div class="box-name">name</div>
|
2026-02-15 18:15:42 +08:00
|
|
|
<div class="box-value">{{ name }}</div>
|
2026-02-17 01:39:59 +08:00
|
|
|
<div class="box-lock">🔒</div>
|
2026-02-15 18:15:42 +08:00
|
|
|
</div>
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
<div class="var-box" :class="{ success: messageType === 'success' }">
|
|
|
|
|
<div class="box-tag let">let</div>
|
|
|
|
|
<div class="box-name">age</div>
|
2026-02-15 18:15:42 +08:00
|
|
|
<div class="box-value">{{ age }}</div>
|
2026-02-17 01:39:59 +08:00
|
|
|
<div class="box-lock">🔓</div>
|
2026-02-15 18:15:42 +08:00
|
|
|
</div>
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
<div class="var-box">
|
|
|
|
|
<div class="box-tag const">const</div>
|
|
|
|
|
<div class="box-name">isStudent</div>
|
2026-02-15 18:15:42 +08:00
|
|
|
<div class="box-value">{{ isStudent }}</div>
|
2026-02-17 01:39:59 +08:00
|
|
|
<div class="box-lock">🔒</div>
|
2026-02-15 18:15:42 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
<div class="message" v-if="showMessage" :class="messageType">
|
2026-02-15 18:15:42 +08:00
|
|
|
{{ showMessage }}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="controls">
|
2026-02-17 01:39:59 +08:00
|
|
|
<button @click="modifyAge" class="btn btn-primary">修改 age</button>
|
|
|
|
|
<button @click="modifyName" class="btn btn-danger">修改 name</button>
|
|
|
|
|
<button @click="reset" class="btn btn-secondary">重置</button>
|
2026-02-15 18:15:42 +08:00
|
|
|
</div>
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
<div class="code-snippet">
|
|
|
|
|
<code>const name = "{{ name }}"</code>
|
|
|
|
|
<code>let age = {{ age }}</code>
|
2026-02-15 18:15:42 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.variable-box-demo {
|
|
|
|
|
border: 1px solid var(--vp-c-border);
|
|
|
|
|
border-radius: 12px;
|
2026-02-17 01:39:59 +08:00
|
|
|
padding: 20px;
|
|
|
|
|
margin: 16px 0;
|
2026-02-15 18:15:42 +08:00
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.demo-header {
|
|
|
|
|
margin-bottom: 16px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.title {
|
|
|
|
|
font-size: 16px;
|
2026-02-15 18:15:42 +08:00
|
|
|
font-weight: 600;
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.boxes-row {
|
2026-02-15 18:15:42 +08:00
|
|
|
display: flex;
|
|
|
|
|
gap: 16px;
|
|
|
|
|
justify-content: center;
|
2026-02-17 01:39:59 +08:00
|
|
|
margin-bottom: 16px;
|
2026-02-15 18:15:42 +08:00
|
|
|
flex-wrap: wrap;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.var-box {
|
|
|
|
|
width: 100px;
|
|
|
|
|
height: 100px;
|
2026-02-15 18:15:42 +08:00
|
|
|
border: 2px solid var(--vp-c-border);
|
2026-02-17 01:39:59 +08:00
|
|
|
border-radius: 10px;
|
2026-02-15 18:15:42 +08:00
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
2026-02-17 01:39:59 +08:00
|
|
|
position: relative;
|
|
|
|
|
background: var(--vp-c-bg);
|
2026-02-15 18:15:42 +08:00
|
|
|
transition: all 0.3s ease;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.var-box.error {
|
|
|
|
|
border-color: #ef4444;
|
|
|
|
|
background: #fef2f2;
|
|
|
|
|
animation: shake 0.4s ease;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.var-box.success {
|
|
|
|
|
border-color: #10b981;
|
|
|
|
|
background: #ecfdf5;
|
|
|
|
|
animation: pulse 0.4s ease;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@keyframes shake {
|
|
|
|
|
0%, 100% { transform: translateX(0); }
|
|
|
|
|
25% { transform: translateX(-4px); }
|
|
|
|
|
75% { transform: translateX(4px); }
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@keyframes pulse {
|
|
|
|
|
0%, 100% { transform: scale(1); }
|
|
|
|
|
50% { transform: scale(1.05); }
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.box-tag {
|
2026-02-15 18:15:42 +08:00
|
|
|
position: absolute;
|
2026-02-17 01:39:59 +08:00
|
|
|
top: -10px;
|
|
|
|
|
padding: 2px 8px;
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
font-size: 10px;
|
|
|
|
|
font-weight: 600;
|
2026-02-15 18:15:42 +08:00
|
|
|
color: white;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.box-tag.const {
|
|
|
|
|
background: #3b82f6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.box-tag.let {
|
|
|
|
|
background: #10b981;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.box-name {
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
margin-bottom: 4px;
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.box-value {
|
2026-02-17 01:39:59 +08:00
|
|
|
font-size: 20px;
|
2026-02-15 18:15:42 +08:00
|
|
|
font-weight: 600;
|
2026-02-17 01:39:59 +08:00
|
|
|
font-family: monospace;
|
2026-02-15 18:15:42 +08:00
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.box-lock {
|
|
|
|
|
position: absolute;
|
|
|
|
|
bottom: 8px;
|
|
|
|
|
font-size: 12px;
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.message {
|
2026-02-15 18:15:42 +08:00
|
|
|
text-align: center;
|
2026-02-17 01:39:59 +08:00
|
|
|
padding: 10px;
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
margin-bottom: 12px;
|
|
|
|
|
font-size: 13px;
|
2026-02-15 18:15:42 +08:00
|
|
|
font-weight: 500;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.message.error {
|
|
|
|
|
background: #fef2f2;
|
|
|
|
|
color: #dc2626;
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.message.success {
|
|
|
|
|
background: #ecfdf5;
|
|
|
|
|
color: #059669;
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.controls {
|
|
|
|
|
display: flex;
|
2026-02-17 01:39:59 +08:00
|
|
|
gap: 8px;
|
2026-02-15 18:15:42 +08:00
|
|
|
justify-content: center;
|
2026-02-17 01:39:59 +08:00
|
|
|
margin-bottom: 12px;
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.btn {
|
|
|
|
|
padding: 8px 14px;
|
2026-02-15 18:15:42 +08:00
|
|
|
border: none;
|
|
|
|
|
border-radius: 6px;
|
2026-02-17 01:39:59 +08:00
|
|
|
font-size: 13px;
|
2026-02-15 18:15:42 +08:00
|
|
|
font-weight: 500;
|
|
|
|
|
cursor: pointer;
|
2026-02-17 01:39:59 +08:00
|
|
|
transition: all 0.2s;
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.btn-primary {
|
2026-02-17 01:39:59 +08:00
|
|
|
background: #3b82f6;
|
2026-02-15 18:15:42 +08:00
|
|
|
color: white;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.btn-danger {
|
2026-02-17 01:39:59 +08:00
|
|
|
background: #ef4444;
|
2026-02-15 18:15:42 +08:00
|
|
|
color: white;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.btn-secondary {
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
color: var(--vp-c-text-1);
|
2026-02-17 01:39:59 +08:00
|
|
|
border: 1px solid var(--vp-c-border);
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.code-snippet {
|
2026-02-15 18:15:42 +08:00
|
|
|
background: #1e1e1e;
|
2026-02-17 01:39:59 +08:00
|
|
|
border-radius: 6px;
|
|
|
|
|
padding: 10px 14px;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
gap: 4px;
|
2026-02-15 18:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
2026-02-17 01:39:59 +08:00
|
|
|
.code-snippet code {
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
font-size: 12px;
|
2026-02-15 18:15:42 +08:00
|
|
|
color: #d4d4d4;
|
|
|
|
|
}
|
|
|
|
|
</style>
|