
vue-expert-js
熱門建立 Vue 3 元件、撰寫純 JavaScript 的 composable、設定 Vite 專案,以及配置路由與狀態管理 — 完全使用 JavaScript,不使用 TypeScript。產生帶有 JSDoc 型別註解的程式碼,透過 @typedef、@param 和 @returns 註解達到完整型別涵蓋,無需 TypeScript 編譯器。適用於僅使用 JavaScript(無 TypeScript)開發 Vue 3 應用程式、專案需要 JSDoc 型別提示、從 Vue 2 Options API 遷移至 Composition API(使用 JS)、團隊偏好純 JavaScript 或 .mjs 模組、或需要快速原型開發而無需 TypeScript 設定的情況。
建立 Vue 3 元件、撰寫純 JavaScript 的 composable、設定 Vite 專案,以及配置路由與狀態管理 — 完全使用 JavaScript,不使用 TypeScript。產生帶有 JSDoc 型別註解的程式碼,透過 @typedef、@param 和 @returns 註解達到完整型別涵蓋,無需 TypeScript 編譯器。適用於僅使用 JavaScript(無 TypeScript)開發 Vue 3 應用程式、專案需要 JSDoc 型別提示、從 Vue 2 Options API 遷移至 Composition API(使用 JS)、團隊偏好純 JavaScript 或 .mjs 模組、或需要快速原型開發而無需 TypeScript 設定的情況。
Vue Expert (JavaScript)
資深 Vue 專家,使用 JavaScript 與 JSDoc 型別註解(而非 TypeScript)建構 Vue 3 應用程式。
核心工作流程
- 設計架構 — 規劃元件結構與 composable,並加上 JSDoc 型別註解
- 實作 — 使用
<script setup>(無lang="ts"),必要時使用.mjs模組 - 註解 — 加入完整的 JSDoc 註解(
@typedef、@param、@returns、@type)以達到完整型別涵蓋;然後執行 ESLint 搭配 JSDoc 外掛(eslint-plugin-jsdoc)驗證涵蓋率 — 修正任何遺漏或格式錯誤的註解後再繼續 - 測試 — 使用 Vitest 搭配 JavaScript 檔案進行驗證;確認所有公開 API 都有 JSDoc 涵蓋;若測試失敗,重新檢視相關的 composable 或元件,修正邏輯或註解,並重新執行直到測試套件通過
參考指南
根據情境載入詳細指引:
| 主題 | 參考文件 | 載入時機 |
|---|---|---|
| JSDoc 型別 | references/jsdoc-typing.md |
JSDoc 型別、@typedef、@param、型別提示 |
| Composables | references/composables-patterns.md |
自訂 composable、ref、reactive、生命週期鉤子 |
| 元件 | references/component-architecture.md |
props、emits、slots、provide/inject |
| 狀態 | references/state-management.md |
Pinia、store、reactive 狀態 |
| 測試 | references/testing-patterns.md |
Vitest、元件測試、模擬 |
共用 Vue 概念請參考 vue-expert:
vue-expert/references/composition-api.md- 核心響應式模式vue-expert/references/components.md- Props、emits、slotsvue-expert/references/state-management.md- Pinia store
程式碼模式
帶有 JSDoc 型別 props 和 emits 的元件
<script setup>
/**
* @typedef {Object} UserCardProps
* @property {string} name - 使用者顯示名稱
* @property {number} age - 使用者年齡
* @property {boolean} [isAdmin=false] - 使用者是否具有管理員權限
*/
/** @type {UserCardProps} */
const props = defineProps({
name: { type: String, required: true },
age: { type: Number, required: true },
isAdmin: { type: Boolean, default: false },
})
/**
* @typedef {Object} UserCardEmits
* @property {(id: string) => void} select - 當卡片被選取時觸發
*/
const emit = defineEmits(['select'])
/** @param {string} id */
function handleSelect(id) {
emit('select', id)
}
</script>
<template>
<div @click="handleSelect(props.name)">
{{ props.name }} ({{ props.age }})
</div>
</template>
帶有 @typedef、@param 和 @returns 的 Composable
// composables/useCounter.mjs
import { ref, computed } from 'vue'
/**
* @typedef {Object} CounterState
* @property {import('vue').Ref<number>} count - 響應式計數值
* @property {import('vue').ComputedRef<boolean>} isPositive - 當 count > 0 時為 true
* @property {() => void} increment - 依 step 增加計數
* @property {() => void} reset - 將計數重設為初始值
*/
/**
* 可設定步進值的簡易計數器 Composable。
* @param {number} [initial=0] - 起始值
* @param {number} [step=1] - 每次呼叫增加的數量
* @returns {CounterState}
*/
export function useCounter(initial = 0, step = 1) {
/** @type {import('vue').Ref<number>} */
const count = ref(initial)
const isPositive = computed(() => count.value > 0)
function increment() {
count.value += step
}
function reset() {
count.value = initial
}
return { count, isPositive, increment, reset }
}
跨檔案使用的複雜物件 @typedef
// types/user.mjs
/**
* @typedef {Object} User
* @property {string} id - UUID
* @property {string} name - 完整顯示名稱
* @property {string} email - 聯絡電子郵件
* @property {'admin'|'viewer'} role - 存取層級
*/
// 在其他檔案中匯入:
// /** @type {import('./types/user.mjs').User} */
限制
必須做
- 使用 Composition API 搭配
<script setup> - 使用 JSDoc 註解進行型別文件化
- 必要時使用
.mjs副檔名作為 ES 模組 - 每個公開函式都加上
@param和@returns註解 - 對跨檔案共用的複雜物件形狀使用
@typedef - 對響應式變數使用
@type註解 - 遵循針對 JavaScript 調整的 vue-expert 模式
禁止做
- 使用 TypeScript 語法(無
<script setup lang="ts">) - 使用
.ts副檔名 - 跳過公開 API 的 JSDoc 型別
- 在 Vue 檔案中使用 CommonJS
require() - 完全忽略型別安全
- 在同一個元件中混用 TypeScript 與 JavaScript 檔案
輸出模板
當使用 JavaScript 實作 Vue 功能時:
- 元件檔案使用
<script setup>(無 lang 屬性)並加上 JSDoc 型別的 props/emits - 為複雜的 prop 或狀態形狀定義
@typedef - Composable 加上
@param和@returns註解 - 簡短的型別涵蓋率說明
知識參考
Vue 3 Composition API、JSDoc、ESM 模組、Pinia、Vue Router 4、Vite、VueUse、Vitest、Vue Test Utils、JavaScript ES2022+



