快速创建 Vue 3 组合式函数
快速创建符合 wot-starter 项目规范的 Vue 3 组合式函数。
src/composables/
├── types/ # 类型定义目录
│ └── theme.ts
├── useGlobalToast.ts # 全局 Toast
├── useGlobalMessage.ts # 全局 Message
├── useGlobalLoading.ts # 全局 Loading
├── useTheme.ts # 主题管理
├── useManualTheme.ts # 手动主题管理
├── useTabbar.ts # TabBar 控制
└── use{Name}.ts # 新建的 composable
// src/composables/use{Name}.ts
import { ref, computed, onMounted, onUnmounted } from 'vue'
/**
* {描述这个 composable 的用途}
* @returns {返回值描述}
*/
export function use{Name}() {
// 响应式状态
const state = ref<StateType>(initialValue)
const loading = ref(false)
// 计算属性
const computedValue = computed(() => {
return state.value
})
// 方法
function doSomething() {
// 业务逻辑
}
async function asyncAction() {
loading.value = true
try {
// 异步操作
} finally {
loading.value = false
}
}
// 生命周期
onMounted(() => {
// 初始化
})
onUnmounted(() => {
// 清理
})
return {
state,
loading,
computedValue,
doSomething,
asyncAction,
}
}
// src/composables/use{Name}.ts
import { computed } from 'vue'
import { storeToRefs } from 'pinia'
export function use{Name}() {
const store = use{Name}Store()
// 使用 storeToRefs 保持响应性
const { data, loading } = storeToRefs(store)
// 封装 action
async function refresh() {
await store.fetchData()
}
return {
data,
loading,
refresh,
}
}
// src/composables/use{Name}.ts
interface Use{Name}Options {
immediate?: boolean
onSuccess?: (data: any) => void
onError?: (error: Error) => void
}
export function use{Name}(options: Use{Name}Options = {}) {
const { immediate = true, onSuccess, onError } = options
const data = ref<DataType | null>(null)
const loading = ref(false)
const error = ref<Error | null>(null)
async function execute() {
loading.value = true
error.value = null
try {
const result = await fetchData()
data.value = result
onSuccess?.(result)
} catch (err) {
error.value = err as Error
onError?.(err as Error)
} finally {
loading.value = false
}
}
if (immediate) {
execute()
}
return {
data,
loading,
error,
execute,
}
}
// 全局 Toast 提示
export const useGlobalToast = defineStore('global-toast', {
actions: {
show(option: ToastOptions | string) { /* ... */ },
success(option: ToastOptions | string) { /* ... */ },
error(option: ToastOptions | string) { /* ... */ },
info(option: ToastOptions | string) { /* ... */ },
warning(option: ToastOptions | string) { /* ... */ },
close() { /* ... */ },
},
})
// 主题管理
export function useTheme() {
const store = useThemeStore()
return {
theme: computed(() => store.theme),
isDark: computed(() => store.isDark),
themeVars: computed(() => store.themeVars),
}
}
<script setup lang="ts">
// 自动导入,无需手动 import
const { data, loading, execute } = use{Name}({
immediate: false,
onSuccess: (data) => {
console.log('成功:', data)
},
})
// 手动执行
const handleClick = () => {
execute()
}
</script>
use{Name}.ts 格式use{Name} 命名types/ 目录或文件顶部storeToRefs 保持响应性npx skills add wot-ui/vue-composable-creator下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer