使用者在畫面上自行調整 MyButton 的樣式與內容,就要再加上一個互動式控制區塊,讓使用者可以操作:
- 下拉選單(select)選擇
size
、round
或 square
- 輸入框(input)設定
label
、color
、textColor
- 然後按鈕會根據這些值即時更新
✅ 修改後的 Lesson3.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| <!-- Lesson3.vue --> <template> <div> <h2>JavaScript 系列九:第3課 ── 學習 Vue 的 events 觀念</h2>
<div class="control-panel"> <label> 按鈕文字: <input v-model="label" /> </label>
<label> 背景顏色: <input type="color" v-model="color" /> </label>
<label> 文字顏色: <input type="color" v-model="textColor" /> </label>
<label> 按鈕大小: <select v-model="size"> <option value="small">小</option> <option value="medium">中</option> <option value="large">大</option> </select> </label>
<label> 圓角樣式: <input type="checkbox" v-model="round" /> </label>
<label> 方形樣式: <input type="checkbox" v-model="square" /> </label> </div>
<MyButton :label="label" :color="color" :textColor="textColor" :size="size" :round="round" :square="square" @click="handleClick(label)" /> </div> </template>
<script setup> import { ref } from 'vue' import MyButton from './Lesson3_components/MyButton.vue'
const label = ref('自訂按鈕') const color = ref('#ff6347') // tomato const textColor = ref('#ffffff') const size = ref('medium') const round = ref(true) const square = ref(false)
function handleClick(name) { alert(`你點擊了:${name}`) } </script>
<style scoped> .control-panel { display: flex; flex-wrap: wrap; gap: 1em; margin-bottom: 1em; } label { display: flex; flex-direction: column; font-size: 0.9rem; } </style>
|
🧪 測試方式
你可以把這段程式貼到 Vue SFC Playground,只要在左側新增 MyButton.vue
,右側是 Lesson3.vue
,即可即時測試互動效果。