商用網站架設實務

LESSON-19 · starter

Vue 3 CDN:createApp、setup、mount

你會依序完成3個本堂專屬微步驟;每一步只修改一個責任,看到結果後才進下一步。 這頁用 可觀察證據anchor 指出下一個動作。

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

開始前確認

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

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

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

本堂焦點:Vue 3 CDN 建立 createAppsetupmount 的最小流程

照這個順序做

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

TODO-1

讓瀏覽器先取得 Vue,再執行 createApp。

目的

瀏覽器先取得 Vue,再執行 createApp

修改檔案

index.html

程式碼位置

Vue CDN script

現在要做:請在 index.html 的「Vue CDN script」位置完成:加入 Vue 3 global CDN 並放在 main.js 前面

貼上位置:index.htmlVue CDN script 區塊,先保留同一檔案的其他內容。

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="main.js" defer></script>

預期結果:NetworkVue CDNmain.js 不再出現 Vue is not defined。

驗收證據:Network CDN 狀態與 ConsoleVue 物件。

卡住時先看這裡

常見錯誤:main.js 放在 CDN 前面。

第一個檢查:先看 Networkscript 順序與狀態碼。

TODO-2

先分開理解 createApp、setup 與 template 可使用的資料;反應式狀態延後到下一堂。

目的

先分開理解 createAppsetuptemplate 可使用的資料;反應式狀態延後到下一堂。

修改檔案

main.js

程式碼位置

createApp setup

現在要做:請在 main.js 的「createApp setup」位置完成:建立 setup 並回傳普通的 title 與 description 字串

貼上位置:main.jscreateApp setup 區塊,先保留同一檔案的其他內容。

const app = Vue.createApp({
  setup() {
    const title = "安心維護服務";
    const description = "讓訪客看懂下一步";
    return { title, description };
  },
});

預期結果:#app 顯示 setup 回傳的 title 與 description,程式不需要先使用 ref

驗收證據:畫面文字、main.jssetup returnVue CDN#app DOM

卡住時先看這裡

常見錯誤:setup 宣告資料卻沒有 return,或太早加入尚未教過的 ref

第一個檢查:只看 setup 內的 constreturn 欄位,確認 template 使用相同名稱。

TODO-3

指定 Vue 應用接手的 DOM 範圍,而不重複建立 mount 容器。

目的

指定 Vue 應用接手的 DOM 範圍,而不重複建立 mount 容器。

修改檔案

index.html

程式碼位置

#app

現在要做:請在 index.html 的「#app」位置完成:把靜態 Vue template 放進唯一的 #app 容器

貼上位置:index.html#app 區塊,先保留同一檔案的其他內容。

<h1>{{ title }}</h1>
<p>{{ description }}</p>

預期結果:#app 內的 Vue template 被渲染,而不是把大括號原樣顯示。

驗收證據:Elements 只有一個 #app,內部出現實際文字。

卡住時先看這裡

常見錯誤:再次建立第二個 #app,或讓 mountselectorHTML id 不一致。

第一個檢查:先搜尋整份 HTML#app 數量,再對照 app.mount 的字串。

階段檔案

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

開啟 starter 的可執行入口

index.htmlVue DOM template:提供唯一 #app、靜態 title/description 與位於容器外的 script

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

<!doctype html>
<html lang="zh-Hant">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>LESSON-19|Vue 3 CDN:starter</title>
  <link rel="icon" href="../../../assets/favicon.svg" type="image/svg+xml">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <main id="app" class="page-shell">
    <p class="eyebrow">LESSON-19 · Vue 3 CDN</p>
    <p class="status-panel">請先完成 TODO-3:在這裡加入 title 與 description 的 Vue template。</p>
  </main>
  <script src="main.js" defer></script>
</body>
</html>
style.css呈現層:控制容器、文字、狀態區、focus 與窄版可讀性。

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

:root {
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", sans-serif;
  color: #17324d;
  background: #f5f8fb;
}
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; line-height: 1.7; }
.page-shell { width: min(100% - 2rem, 760px); margin: 2rem auto; padding: 2rem; border: 1px solid #cbd8e4; border-radius: 20px; background: #fff; }
.eyebrow { color: #176b87; font-weight: 800; letter-spacing: .05em; }
.page-description { color: #52657d; }
.status-panel { min-height: 2rem; padding: .8rem; border-radius: 10px; background: #e8f5f7; color: #17643d; font-weight: 700; }
a:focus-visible { outline: 3px solid #1d4ed8; outline-offset: 3px; }
@media (max-width: 560px) { .page-shell { margin: 1rem auto; padding: 1rem; } h1 { font-size: 1.6rem; } }
main.jsVue 啟動流程:依序觀察 global API、createAppsetupreturnmount

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

const VueApi = window.Vue;

// TODO-2:在 createApp 內建立 setup,回傳 title 與 description。
const app = VueApi.createApp({});

app.mount("#app");

完成驗收

下一步

Starter 完成後,不要直接看 Solution;先前往 Checkpoint,自己找出最後一個核心缺口。

前往 Checkpoint,自己補最後一個核心缺口