商用網站架設實務

LESSON-22 · checkpoint

用 v-model、watch 與生命週期整理 Vue 表單狀態

主要結構已經完成,只留下本堂一個核心缺口。先預測,再從指定檔案找出它。 這頁用 可觀察證據 和 anchor 指出下一個動作。

Starter3 個小成果
Checkpoint一個核心缺口
Solution4 個差異

開始前確認

這一頁的結果:入口頁可以開啟

工作方式:先把這一階段複製到自己的工作資料夾,開啟 files/index.html,記下修改前的畫面、DOM、Console 或 Network狀態。

會用到的檔案:index.html、style.css、main.js

本堂焦點:用 v-model、watch 與生命週期整理 Vue 表單狀態

照這個順序做

一次只做一張卡。完成後先重新整理入口,再留下證據,才進入下一張。

CHECKPOINT-1

讓使用者修正輸入後重試。

目的

讓使用者修正輸入後重試。

修改檔案

main.js

程式碼位置

error reset

現在要做:請在 main.js 的「error reset」完成:修正錯誤後無法回 idle

貼上位置:main.js:error reset 區塊。

if (status.value === "error") status.value = "idle";

預期結果:error to idle to loading to success 可重現。

驗收證據:status、aria-busy、disabled 與 focus。

卡住時先看這裡

常見錯誤:一次修改太多責任。

第一個檢查:只檢查目前指定的檔案與區塊。

階段檔案

完整檔案收在可展開的卡片中;先完成上方微步驟,再用這裡查閱與複製。檔案位置要和目前的相對路徑一致。

開啟 checkpoint 的可執行入口

index.htmlP0 專章可執行入口。

這段要貼在哪裡:先在自己的工作資料夾建立同名相對位置,再複製內容。

​
<!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>LESSON-22 checkpoint</title><link rel="stylesheet" href="style.css"></head><body><main id="form-app" v-cloak><h1>Vue 表單狀態</h1><form @submit.prevent="submitForm" :aria-busy="status==='loading'"><label>姓名 <input ref="nameInput" v-model.trim="form.name" autocomplete="name"></label><label>預算 <input v-model.number="form.budget" inputmode="numeric"></label><button :disabled="status==='loading'" type="submit">{{ status==='loading' ? '送出中' : '送出' }}</button><p ref="errorBox" tabindex="-1" role="status">{{ message }}</p></form></main><script src="https://unpkg.com/vue@3.5.40/dist/vue.global.js"></script><script src="main.js" defer></script></body></html>
style.cssP0 專章樣式與狀態證據。

這段要貼在哪裡:先在自己的工作資料夾建立同名相對位置,再複製內容。

​
*{box-sizing:border-box}body{margin:0;font-family:system-ui,-apple-system,"Segoe UI","Noto Sans TC",sans-serif;line-height:1.7;color:#17324d;background:#eef5f8}body>main,body>header,body>footer,main{width:min(100% - 2rem,760px);margin:1rem auto;padding:1rem}a{color:#155e75}button,input,textarea{font:inherit}button{padding:.65rem .9rem}input,textarea{display:block;width:100%;padding:.6rem}@media(max-width:390px){body>main,body>header,body>footer,main{width:min(100% - 1rem,760px)}}
[v-cloak]{display:none}[aria-busy="true"]{opacity:.7}:focus-visible{outline:3px solid #1d4ed8;outline-offset:3px}
main.jsP0 專章互動與 Console 證據。

這段要貼在哪裡:先在自己的工作資料夾建立同名相對位置,再複製內容。

​
const {createApp,ref,reactive,computed,onMounted,nextTick}=Vue;createApp({setup(){const form=reactive({name:"",budget:0});const status=ref("idle");const nameInput=ref(null);const errorBox=ref(null);const message=computed(()=>({idle:"尚未送出",loading:"正在處理",success:"測試送出成功",error:"請先輸入姓名"})[status.value]);/* CHECKPOINT-1:修正姓名後仍停在 error,尚未用 watch 回到 idle。 */onMounted(()=>nameInput.value?.focus());async function submitForm(){if(!form.name){status.value="error";await nextTick();errorBox.value?.focus();return;}status.value="loading";window.setTimeout(()=>{status.value="success";},350);}return{form,status,message,nameInput,errorBox,submitForm};}}).mount("#form-app");

完成驗收

下一步

Checkpoint 完成後,先說出你修正的原因,再前往 Solution 對照每個檔案差異。

前往 Solution,逐檔對照差異