商用網站架設實務

LESSON-20 · solution

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

這一頁不是直接複製答案;先逐檔看差異、修改原因與驗收證據,再把需要的改動帶回自己的資料夾。 這頁用 可觀察證據anchor 指出下一個動作。

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

開始前確認

這一頁的結果:所有相關檔案可開啟或閱讀;沒有 TODOCHECKPOINT 或 placeholder;能說明每個檔案為何改動

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

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

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

照這個順序做

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

差異 1

建立 ref 的兩種讀取語境。

目的

建立 ref 的兩種讀取語境。

修改檔案

main.js

程式碼位置

ref and .value

現在要做:先對照差異,再檢查 在 main.js 的「ref and .value」對照這項變更:const keyword = ref("");

貼上位置:main.jsref and .value 區塊

const keyword = ref("");

預期結果:關鍵字與畫面同步。

驗收證據:main.js、輸入值與畫面。

卡住時先看這裡

常見錯誤:只複製完整答案,卻沒有先理解這一項差異的責任。

第一個檢查:只對照目前差異指定的檔案與 anchor。

差異 2

不建立第二份可變清單。

目的

不建立第二份可變清單。

修改檔案

main.js

程式碼位置

computed

現在要做:先對照差異,再檢查 在 main.js 的「computed」對照這項變更:const filteredServices = computed(() => services.value.filter((item) => item.name.includes(keyword.value)));

貼上位置:main.jscomputed 區塊

const filteredServices = computed(() => services.value.filter((item) => item.name.includes(keyword.value)));

預期結果:搜尋時清單與數量同步。

驗收證據:畫面、Consolecomputed callback。

卡住時先看這裡

常見錯誤:只複製完整答案,卻沒有先理解這一項差異的責任。

第一個檢查:只對照目前差異指定的檔案與 anchor。

差異 3

建立可預測清單與顯隱。

目的

建立可預測清單與顯隱。

修改檔案

index.html

程式碼位置

v-for key v-if v-show

現在要做:先對照差異,再檢查 在 index.html 的「v-for key v-if v-show」對照這項變更:<li v-for="service in filteredServices" :key="service.id">{{ service.name }}</li>

貼上位置:index.htmlv-for key v-if v-show 區塊

<li v-for="service in filteredServices" :key="service.id">{{ service.name }}</li>

預期結果:Console 無 key 警告,空結果可見。

驗收證據:ElementsConsole 與畫面狀態。

卡住時先看這裡

常見錯誤:只複製完整答案,卻沒有先理解這一項差異的責任。

第一個檢查:只對照目前差異指定的檔案與 anchor。

差異 4

讓 keyword 改變可重新計算。

目的

讓 keyword 改變可重新計算。

修改檔案

main.js

程式碼位置

computed callback

現在要做:先對照差異,再檢查 在 main.js 的「computed callback」對照這項變更:keyword.value

貼上位置:main.jscomputed callback 區塊

keyword.value

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

驗收證據:畫面、Consolemain.js

卡住時先看這裡

常見錯誤:只複製完整答案,卻沒有先理解這一項差異的責任。

第一個檢查:只對照目前差異指定的檔案與 anchor。

階段檔案

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

開啟 solution 的可執行入口

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 solution</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.prod.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.value)));return{keyword,showList,filteredServices};}}).mount("#services-app");

完成驗收

下一步

Solution 對照完成後,回到 README 整理證據,並準備下一堂課的先備知識。

回到 README,整理本堂證據並銜接下一堂

繼續自主學習

開啟 examples/supplement/index.html;自學檔案不混入 Solution files。