Demo.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <div class="hello-demo">
  3. <el-card>
  4. <template #header>
  5. <div class="card-header">
  6. <span>🎮 功能演示</span>
  7. <el-tag type="warning">Keep-Alive 页面</el-tag>
  8. </div>
  9. </template>
  10. <div class="demo-sections">
  11. <!-- 示例 1: 基础交互 -->
  12. <div class="demo-section">
  13. <h3>🔘 基础交互</h3>
  14. <el-space wrap>
  15. <el-button @click="count++">点击次数:{{ count }}</el-button>
  16. <el-button type="primary" @click="showMessage">显示消息</el-button>
  17. <el-button type="success" @click="showSuccess">成功提示</el-button>
  18. </el-space>
  19. </div>
  20. <!-- 示例 2: 表单演示 -->
  21. <div class="demo-section">
  22. <h3>📝 表单演示</h3>
  23. <el-form :model="formData" label-width="100px" style="max-width: 500px;">
  24. <el-form-item label="用户名">
  25. <el-input v-model="formData.username" placeholder="请输入用户名" />
  26. </el-form-item>
  27. <el-form-item label="邮箱">
  28. <el-input v-model="formData.email" placeholder="请输入邮箱" />
  29. </el-form-item>
  30. <el-form-item label="备注">
  31. <el-input
  32. v-model="formData.note"
  33. type="textarea"
  34. :rows="3"
  35. placeholder="请输入备注"
  36. />
  37. </el-form-item>
  38. <el-form-item>
  39. <el-button type="primary" @click="submitForm">提交表单</el-button>
  40. <el-button @click="resetForm">重置表单</el-button>
  41. </el-form-item>
  42. </el-form>
  43. </div>
  44. <!-- 示例 3: 数据表格 -->
  45. <div class="demo-section">
  46. <h3>📊 数据表格</h3>
  47. <el-table :data="tableData" style="width: 100%" border stripe>
  48. <el-table-column prop="id" label="ID" width="80" />
  49. <el-table-column prop="name" label="姓名" />
  50. <el-table-column prop="age" label="年龄" width="80" />
  51. <el-table-column prop="email" label="邮箱" />
  52. <el-table-column label="操作" width="150" fixed="right">
  53. <template #default="{ row }">
  54. <el-button size="small" type="primary" @click="handleEdit(row)">编辑</el-button>
  55. <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
  56. </template>
  57. </el-table-column>
  58. </el-table>
  59. </div>
  60. <!-- 示例 4: Keep-Alive 测试 -->
  61. <div class="demo-section">
  62. <h3>⏸️ Keep-Alive 测试</h3>
  63. <el-alert
  64. title="此页面启用了 keep-alive,切换路由后状态会保留"
  65. type="info"
  66. :closable="false"
  67. show-icon
  68. />
  69. <p style="margin-top: 10px; color: #606266;">
  70. 当前输入的内容:<strong>{{ formData.username || '无' }}</strong>
  71. </p>
  72. <p style="color: #606266;">
  73. 点击次数:<strong>{{ count }}</strong>
  74. </p>
  75. </div>
  76. </div>
  77. </el-card>
  78. </div>
  79. </template>
  80. <script setup>
  81. import { ref } from 'vue'
  82. import { ElMessage, ElMessageBox } from 'element-plus'
  83. // 基础交互
  84. const count = ref(0)
  85. const showMessage = () => {
  86. ElMessage.info('这是一个普通消息提示')
  87. }
  88. const showSuccess = () => {
  89. ElMessage.success('操作成功!')
  90. }
  91. // 表单演示
  92. const formData = ref({
  93. username: '',
  94. email: '',
  95. note: ''
  96. })
  97. const submitForm = () => {
  98. if (!formData.value.username) {
  99. ElMessage.warning('请输入用户名')
  100. return
  101. }
  102. ElMessage.success(`表单已提交,用户名:${formData.value.username}`)
  103. }
  104. const resetForm = () => {
  105. formData.value = {
  106. username: '',
  107. email: '',
  108. note: ''
  109. }
  110. }
  111. // 数据表格
  112. const tableData = ref([
  113. { id: 1, name: '张三', age: 25, email: 'zhangsan@example.com' },
  114. { id: 2, name: '李四', age: 30, email: 'lisi@example.com' },
  115. { id: 3, name: '王五', age: 28, email: 'wangwu@example.com' }
  116. ])
  117. const handleEdit = (row) => {
  118. ElMessage.info(`编辑:${row.name}`)
  119. }
  120. const handleDelete = (row) => {
  121. ElMessageBox.confirm(`确定要删除 ${row.name} 吗?`, '提示', {
  122. confirmButtonText: '确定',
  123. cancelButtonText: '取消',
  124. type: 'warning'
  125. }).then(() => {
  126. ElMessage.success('删除成功')
  127. }).catch(() => {
  128. ElMessage.info('已取消删除')
  129. })
  130. }
  131. </script>
  132. <style scoped>
  133. .hello-demo {
  134. padding: 20px;
  135. }
  136. .card-header {
  137. display: flex;
  138. justify-content: space-between;
  139. align-items: center;
  140. }
  141. .demo-sections {
  142. display: flex;
  143. flex-direction: column;
  144. gap: 30px;
  145. }
  146. .demo-section {
  147. padding-bottom: 20px;
  148. border-bottom: 1px solid #ebeef5;
  149. }
  150. .demo-section:last-child {
  151. border-bottom: none;
  152. }
  153. .demo-section h3 {
  154. margin-bottom: 15px;
  155. color: #303133;
  156. }
  157. </style>