參考:

SFC 是開發網站專案的標準做法
Using *.vue files for our Vue project makes sense because:
  1. it becomes easier to handle larger projects with the use of templates and components.
  2. we can see and test our project through the https protocol, like the users will see the page.
  3. the page updates immediately when changes are saved, without reloading.
  4. this is how real web pages in Vue are built.
  5. it is how developers work.
重點

  1. 建立專案資料夾
  2. 1.
    安裝環境
    包括
    1. VS Code Editor (寫程式的整合開發環境)
    2. VS Code "Volar" Extension
      highlighting and auto-completion with *.vue files

    3. Node.js
      server-side JavaScript runtime environment.
      the Vue build tool "Vite" runs on top of this.
  3. 2.
    建立專案資料夾
    放所有相關的檔案
    1. 建立資料夾,例如 ./myFirstSFC
    2. 建立框架 (Scarffold) ~ npm init vue@latest
      1. npm 是 Node.js 的預設套件管理工具 [FAQ]
      2. Answer "No" to all options:
    3. 初始化環境
      1. cd sfc2
      2. npm install:Install all required dependencies so that the Vue project works:
      3. npm run dev:Start the development server
  4. 3.
    進入 Vite 前端建構工具
    順利完成環境建立後,就會進入 Vite 前端建構工具(build tool),由 Vue.js 作者尤雨溪團隊開發,主要用來提升前端開發的效率和體驗。 [FAQ]

    小提醒:
    如果沒有自動開啟瀏覽器,就直接輸入網址


  5. 專案檔案說明 (The Project Files)
  6. 4.
    main.js
    位置:
    ./src/main.js

    目的:
    tells Vite how to build the Vue project based on the "App.vue" file.
    類似一開始寫 Vue 的主要框架
    <div id="app"></div>
    
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    
    <script>
    
      const app = Vue.createApp({
        data() {
          return {
            message: "Hello World!"
          }
        }
      })
    
     app.mount('#app')
    
    </script>

  7. 5.
    App.vue
    Vue 的主要框架,3 個部分
    1. <script>
    2. <template>
    3. <style>

    小提醒:
    1. script 還包括 import 其他 Vue 的 components (放在 ./components 資料夾內)
    2. templete 裡面的 component 參考 <HelloWorld msg="You did it!" /> <TheWelcome />


  8. 開始寫程式囉
  9. 6.
    從零開始 ~ 清除成 empty project
    依序處理
    1. 刪除 App.vue 所有的內容,只留下基本的框 (圖1)
    2. 刪除資料夾 ./src/assets 和 components (圖2)  // src 資料夾只剩下 App.vue 和 main.js
    3. 刪除 main.js 內的 import './assets/main.css'  // 前面都刪光了,已經沒得參考

    圖1:
    795741c56e550a901a3bd4ba6544895f.png


    圖2:
    77401db48e221099784c882abec9d5ab.png

  10. 7.
    秀 Hello World!
    在哪裡寫呢?
    想也知道,一定是在 App.vue 的 templete 中

    原因很簡單
    1. 只剩下 App.vue,而且是主要框 (當然也可以放在 component,以後再說囉,第一支程式嘛)
    2. 和 html 有關的,就在 templete 
  11. 8.
    Save 後立刻看到結果!
    這就是 Vite 的好處,更快速友善的開發環境!


  12. 再熟悉一下 Vue
  13. 9.
    試試看變數 {{ message }}
    怎麼寫呢?這簡單,一開始就練過
    1. 在 hml  (template ) 的地方 改成 {{ message }}
    2. 在 script 的地方用 Vue 的變數 data() return  message 就對了


    小技巧:
    要換別的顯示資訊,例如改成 hi
    和前面的 Hello World 不一樣,才看得出修改後的有沒有影響喔 
    評語
    請登入後才可以評分
    位置
    資料夾名稱
    Vue.js 學習筆記
    上傳者
    蘇德宙
    單位
    台灣數位員工
    建立
    2025-07-29 15:28:06
    最近修訂
    2025-07-29 16:55:24