商用網站架設實務

LESSON-20 · checkpoint

用 ref、computed、v-for 與 v-if 管理服務清單畫面

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

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

開始前確認

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

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

會用到的檔案:index.htmlstyle.cssmain.js

本堂焦點:refcomputedv-forv-if 管理服務清單畫面

照這個順序做

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

CHECKPOINT-1

讓 keyword 改變可重新計算。

目的

讓 keyword 改變可重新計算。

修改檔案

main.js

程式碼位置

computed callback

現在要做:請在 main.js 的「computed callback」完成:補回遺漏的 .value

貼上位置:main.jscomputed callback 區塊。

keyword.value

預期結果:清單可依關鍵字正確更新。

驗收證據:畫面、Consolemain.js

卡住時先看這裡

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

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

階段檔案

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

開啟 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-20 checkpoint</title><link rel="stylesheet" href="style.css"></head><body><main id="services-app" v-cloak><h1>Vue 反應式渲染</h1><label>搜尋 <input v-model.trim="keyword"></label><button type="button" @click="showList=!showList">{{ showList ? '隱藏' : '顯示' }}清單</button><p>共 {{ filteredServices.length }} 項</p><ul v-if="showList"><li v-for="service in filteredServices" :key="service.id">{{ service.name }}</li></ul><p v-if="filteredServices.length===0">沒有結果</p></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}:focus-visible{outline:3px solid #1d4ed8;outline-offset:3px}
main.jsP0 專章互動與 Console 證據。

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

const {createApp,ref,computed}=Vue;createApp({setup(){const keyword=ref("");const showList=ref(true);const services=ref([{id:"plan",name:"網站規劃"},{id:"content",name:"內容整理"}]);const filteredServices=computed(()=>services.value.filter((item)=>item.name.includes(keyword)));/* CHECKPOINT-1:computed callback 忘記讀 keyword.value。 */return{keyword,showList,filteredServices};}}).mount("#services-app");

完成驗收

下一步

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

前往 Solution,逐檔對照差異