商用網站架設實務

LESSON-19 · checkpoint

Vue 3 CDN:createApp、setup、mount

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

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

開始前確認

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

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

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

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

照這個順序做

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

CHECKPOINT-1

讓建立好的 application instance 真正接手畫面。

目的

讓建立好的 application instance 真正接手畫面。

修改檔案

main.js

程式碼位置

app.mount

現在要做:請在 main.js 的「app.mount」位置完成:補上 app.mount("#app")

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

app.mount("#app");

預期結果:#app 內不再顯示原始 template 語法。

驗收證據:畫面、#app DOMConsolemount 相關錯誤。

卡住時先看這裡

常見錯誤:只呼叫 createApp,忘記 mount

第一個檢查:只搜尋 main.js 最後是否有 mount

階段檔案

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

開啟 checkpoint 的可執行入口

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:checkpoint</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>
    <h1>{{ title }}</h1>
    <p class="page-description">{{ description }}</p>
    <p class="status-panel">Vue template 已準備好,請觀察 mount 前後的差異。</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.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;

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

// CHECKPOINT-1:Vue app 已建立,但尚未掛載到 #app。

完成驗收

下一步

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

前往 Solution,逐檔對照差異