vue-best-practices by dedalus-erp-pas/foundation-skills
npx skills add https://github.com/dedalus-erp-pas/foundation-skills --skill vue-best-practicesVue.js 3 应用程序的全面最佳实践指南。包含跨多个类别的指导原则,以确保编写地道、可维护和可扩展的 Vue.js 代码,包括用于实用优先样式的 Tailwind CSS 集成模式以及 PrimeVue 组件库的最佳实践。
在以下情况下参考这些指南:
| 类别 | 重点 | 前缀 |
|---|---|---|
| 组合式 API | 正确使用组合式 API 模式 | composition- |
| 组件设计 | 组件结构和组织 | component- |
| 响应式 | 响应式状态管理模式 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
reactive-| Props 和事件 | 组件通信模式 | props- |
| 模板模式 | 模板语法最佳实践 | template- |
| 代码组织 | 项目和代码结构 | organization- |
| TypeScript | 类型安全的 Vue.js 模式 | typescript- |
| 错误处理 | 错误边界和处理 | error- |
| Tailwind CSS | 实用优先的样式模式 | tailwind- |
| PrimeVue | 组件库集成模式 | primevue- |
composition-script-setup - 始终为单文件组件使用 <script setup>composition-ref-vs-reactive - 对原始值使用 ref(),对对象使用 reactive()composition-computed-derived - 对所有派生状态使用 computed()composition-watch-side-effects - 仅对副作用使用 watch()/watchEffect()composition-composables - 将可重用逻辑提取到组合式函数中composition-lifecycle-order - 将生命周期钩子放在响应式状态声明之后composition-avoid-this - 在组合式 API 中切勿使用 thiscomponent-single-responsibility - 一个组件,一个职责component-naming-convention - 组件使用 PascalCase,模板中使用 kebab-casecomponent-small-focused - 保持组件在 200 行以内component-presentational-container - 在有益时将逻辑与展示分离component-slots-flexibility - 使用插槽实现灵活的组件组合component-expose-minimal - 仅通过 defineExpose() 暴露必要内容reactive-const-refs - 始终使用 const 声明 refsreactive-unwrap-template - 让 Vue 在模板中解包 refs(不使用 .value)reactive-shallow-large-data - 对大型非响应式数据使用 shallowRef()/shallowReactive()reactive-readonly-props - 使用 readonly() 防止变更reactive-toRefs-destructure - 解构响应式对象时使用 toRefs()reactive-avoid-mutation - 对于复杂状态,优先使用不可变更新props-define-types - 始终使用 defineProps<T>() 定义 prop 类型props-required-explicit - 明确指定必需与可选 propsprops-default-values - 使用 withDefaults() 提供合理的默认值props-immutable - 切勿直接变更 propsprops-validation - 使用验证器函数进行复杂的 prop 验证events-define-emits - 始终使用 defineEmits<T>() 定义 emitsevents-naming - 在模板中对事件名称使用 kebab-caseevents-payload-objects - 为包含多个值的事件传递对象template-v-if-v-show - 条件渲染使用 v-if,切换显示使用 v-showtemplate-v-for-key - 始终为 v-for 使用唯一且稳定的 :keytemplate-v-if-v-for - 切勿在同一元素上同时使用 v-if 和 v-fortemplate-computed-expressions - 将复杂表达式移至计算属性template-event-modifiers - 适当使用事件修饰符(.prevent、.stop)template-v-bind-shorthand - 使用简写语法(: 代表 v-bind,@ 代表 v-on)template-v-model-modifiers - 使用 v-model 修饰符(.trim、.number、.lazy)organization-feature-folders - 按功能而非类型组织organization-composables-folder - 将组合式函数保存在专用的 composables/ 文件夹中organization-barrel-exports - 使用索引文件实现简洁的导入organization-consistent-naming - 遵循一致的命名约定organization-colocation - 将相关文件放在一起(组件、测试、样式)typescript-generic-components - 对可重用的类型化组件使用泛型typescript-prop-types - 使用 TypeScript 接口定义 proptypescript-emit-types - 显式地为 emit 载荷指定类型typescript-ref-typing - 当无法推断时,为 refs 指定类型typescript-template-refs - 使用 ref<InstanceType<typeof Component> | null>(null) 为模板 refs 指定类型error-boundaries - 使用 onErrorCaptured() 实现组件错误边界error-async-handling - 显式处理异步操作中的错误error-provide-fallbacks - 为错误状态提供备用 UIerror-logging - 适当地记录错误以便调试tailwind-utility-first - 直接在模板中应用实用类,避免自定义 CSStailwind-class-order - 使用一致的类排序(布局 → 间距 → 排版 → 视觉)tailwind-responsive-mobile-first - 使用移动优先的响应式设计(sm:、md:、lg:)tailwind-component-extraction - 将重复的实用模式提取到 Vue 组件中tailwind-dynamic-classes - 使用计算属性或辅助函数处理动态类tailwind-complete-class-strings - 始终使用完整的类字符串,切勿拼接tailwind-state-variants - 使用状态变体(hover:、focus:、active:)处理交互tailwind-dark-mode - 使用 dark: 前缀支持深色模式tailwind-design-tokens - 在 Tailwind 配置中配置设计令牌以确保一致性tailwind-avoid-apply-overuse - 限制 @apply 的使用;优先使用 Vue 组件进行抽象primevue-design-tokens - 使用设计令牌而非 CSS 覆盖来实现主题化primevue-passthrough-api - 使用 PassThrough (pt) API 自定义组件primevue-wrapper-components - 包装 PrimeVue 组件以实现跨应用的一致样式primevue-unstyled-mode - 使用无样式模式配合 Tailwind 以获得完整的样式控制primevue-global-pt-config - 在应用级别定义共享的 PassThrough 属性primevue-merge-strategies - 为 PT 自定义选择合适的合并策略primevue-use-passthrough-utility - 使用 usePassThrough 扩展预设primevue-typed-components - 利用 PrimeVue 的 TypeScript 支持实现类型安全primevue-accessibility - 使用适当的 aria 属性保持 WCAG 合规性primevue-lazy-loading - 对大型 PrimeVue 导入使用异步组件组合式 API 是 Vue.js 3 的推荐方法。遵循以下模式:
<script setup>:更简洁,TypeScript 推断更好,性能更优use 前缀约定(例如 useAuth、useFetch)设计良好的组件是可维护 Vue 应用程序的基础:
理解 Vue 的响应式系统至关重要:
ref();对将要变更的对象使用 reactive()computed()watch()toRefs() 的情况下解构响应式对象正确的组件通信确保代码可维护:
defineProps<T>()defineEmits<T>() 实现类型安全的事件处理modelValue prop 和 update:modelValue emit<script setup> 的推荐结构:
<script setup lang="ts">
// 1. 导入
import { ref, computed, watch, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import type { User } from '@/types'
// 2. Props 和 Emits
const props = defineProps<{
userId: string
initialData?: User
}>()
const emit = defineEmits<{
submit: [user: User]
cancel: []
}>()
// 3. 组合式函数
const router = useRouter()
const { user, loading, error } = useUser(props.userId)
// 4. 响应式状态
const formData = ref({ name: '', email: '' })
const isEditing = ref(false)
// 5. 计算属性
const isValid = computed(() => {
return formData.value.name.length > 0 && formData.value.email.includes('@')
})
// 6. 侦听器(仅用于副作用)
watch(() => props.userId, (newId) => {
fetchUserData(newId)
})
// 7. 方法
function handleSubmit() {
if (isValid.value) {
emit('submit', formData.value)
}
}
// 8. 生命周期钩子
onMounted(() => {
if (props.initialData) {
formData.value = { ...props.initialData }
}
})
</script>
正确:结构良好的组合式函数
// composables/useUser.ts
import { ref, computed, watch } from 'vue'
import type { Ref } from 'vue'
import type { User } from '@/types'
export function useUser(userId: Ref<string> | string) {
// 状态
const user = ref<User | null>(null)
const loading = ref(false)
const error = ref<Error | null>(null)
// 计算属性
const fullName = computed(() => {
if (!user.value) return ''
return `${user.value.firstName} ${user.value.lastName}`
})
// 方法
async function fetchUser(id: string) {
loading.value = true
error.value = null
try {
const response = await api.getUser(id)
user.value = response.data
} catch (e) {
error.value = e as Error
} finally {
loading.value = false
}
}
// 当 userId 变化时自动获取(如果是响应式的)
if (isRef(userId)) {
watch(userId, (newId) => fetchUser(newId), { immediate: true })
} else {
fetchUser(userId)
}
// 返回
return {
user: readonly(user),
fullName,
loading: readonly(loading),
error: readonly(error),
refresh: () => fetchUser(unref(userId))
}
}
正确:带默认值的类型化 props
<script setup lang="ts">
interface Props {
title: string
size?: 'sm' | 'md' | 'lg'
disabled?: boolean
items?: string[]
}
const props = withDefaults(defineProps<Props>(), {
size: 'md',
disabled: false,
items: () => [] // 对数组/对象使用工厂函数
})
</script>
正确:带载荷的类型化 emits
<script setup lang="ts">
interface FormData {
name: string
email: string
}
const emit = defineEmits<{
submit: [data: FormData]
cancel: []
'update:modelValue': [value: string]
}>()
function handleSubmit(data: FormData) {
emit('submit', data)
}
</script>
正确:使用 defineModel 的自定义 v-model (Vue 3.4+)
<script setup lang="ts">
const model = defineModel<string>({ required: true })
// 或带默认值
const modelWithDefault = defineModel<string>({ default: '' })
</script>
<template>
<input :value="model" @input="model = $event.target.value" />
</template>
正确:自定义 v-model (Vue 3.3 及更早版本)
<script setup lang="ts">
const props = defineProps<{
modelValue: string
}>()
const emit = defineEmits<{
'update:modelValue': [value: string]
}>()
const value = computed({
get: () => props.modelValue,
set: (val) => emit('update:modelValue', val)
})
</script>
<template>
<input v-model="value" />
</template>
正确:类型化的模板 refs
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import MyComponent from './MyComponent.vue'
// DOM 元素 ref
const inputRef = ref<HTMLInputElement | null>(null)
// 组件 ref
const componentRef = ref<InstanceType<typeof MyComponent> | null>(null)
onMounted(() => {
inputRef.value?.focus()
componentRef.value?.someExposedMethod()
})
</script>
<template>
<input ref="inputRef" />
<MyComponent ref="componentRef" />
</template>
正确:类型安全的 provide/inject
// types/injection-keys.ts
import type { InjectionKey, Ref } from 'vue'
import type { User } from './user'
export const UserKey: InjectionKey<Ref<User>> = Symbol('user')
// 父组件
import { provide, ref } from 'vue'
import { UserKey } from '@/types/injection-keys'
const user = ref<User>({ id: '1', name: 'John' })
provide(UserKey, user)
// 子组件
import { inject } from 'vue'
import { UserKey } from '@/types/injection-keys'
const user = inject(UserKey)
if (!user) {
throw new Error('User not provided')
}
正确:使用 onErrorCaptured 的错误边界
<script setup lang="ts">
import { ref, onErrorCaptured } from 'vue'
const error = ref<Error | null>(null)
onErrorCaptured((err) => {
error.value = err
// 返回 false 以停止错误传播
return false
})
function reset() {
error.value = null
}
</script>
<template>
<div v-if="error" class="error-boundary">
<p>出错了: {{ error.message }}</p>
<button @click="reset">重试</button>
</div>
<slot v-else />
</template>
正确:带加载/错误状态的异步组件
import { defineAsyncComponent } from 'vue'
const AsyncDashboard = defineAsyncComponent({
loader: () => import('./Dashboard.vue'),
loadingComponent: LoadingSpinner,
errorComponent: ErrorDisplay,
delay: 200, // 200 毫秒后显示加载状态
timeout: 10000 // 10 秒后超时
})
Vue 基于组件的架构与 Tailwind 的实用优先方法自然契合。遵循这些模式以实现可维护、一致的样式。
直接在 Vue 模板中应用 Tailwind 实用类,以实现快速、一致的样式:
正确:模板中的实用类
<template>
<div class="mx-auto max-w-md rounded-xl bg-white p-6 shadow-lg">
<h2 class="text-xl font-semibold text-gray-900">{{ title }}</h2>
<p class="mt-2 text-gray-600">{{ description }}</p>
<button class="mt-4 rounded-md bg-blue-600 px-4 py-2 text-white hover:bg-blue-700">
{{ buttonText }}
</button>
</div>
</template>
保持一致的类排序以提高可读性。推荐顺序:
flex、grid、block、hiddenrelative、absolute、fixedw-、h-、m-、p-text-、font-、leading-bg-、border-、rounded-、shadow-hover:、focus:、active:使用官方的 Prettier 插件(prettier-plugin-tailwindcss)自动排序类。
使用 Tailwind 的响应式前缀实现移动优先的响应式设计:
正确:移动优先的响应式布局
<template>
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
<article
v-for="item in items"
:key="item.id"
class="p-4 text-sm sm:p-6 sm:text-base lg:text-lg"
>
<h3 class="font-medium">{{ item.title }}</h3>
</article>
</div>
</template>
断点参考:
sm: - 640px 及以上md: - 768px 及以上lg: - 1024px 及以上xl: - 1280px 及以上2xl: - 1536px 及以上对交互元素使用状态变体:
正确:按钮的状态变体
<template>
<button
class="rounded-lg bg-indigo-600 px-4 py-2 font-medium text-white
transition-colors duration-150
hover:bg-indigo-700
focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2
active:bg-indigo-800
disabled:cursor-not-allowed disabled:opacity-50"
:disabled="isLoading"
>
{{ isLoading ? '加载中...' : '提交' }}
</button>
</template>
使用 dark: 前缀实现深色模式样式:
正确:深色模式支持
<template>
<div class="bg-white dark:bg-gray-900">
<h1 class="text-gray-900 dark:text-white">{{ title }}</h1>
<p class="text-gray-600 dark:text-gray-400">{{ content }}</p>
<div class="border-gray-200 dark:border-gray-700 rounded-lg border p-4">
<slot />
</div>
</div>
</template>
使用计算属性进行条件类绑定:
正确:用于变体的计算类
<script setup lang="ts">
import { computed } from 'vue'
type ButtonVariant = 'primary' | 'secondary' | 'danger'
type ButtonSize = 'sm' | 'md' | 'lg'
const props = withDefaults(defineProps<{
variant?: ButtonVariant
size?: ButtonSize
}>(), {
variant: 'primary',
size: 'md'
})
const variantClasses = computed(() => {
const variants: Record<ButtonVariant, string> = {
primary: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500',
secondary: 'bg-gray-200 text-gray-900 hover:bg-gray-300 focus:ring-gray-500',
danger: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500'
}
return variants[props.variant]
})
const sizeClasses = computed(() => {
const sizes: Record<ButtonSize, string> = {
sm: 'px-3 py-1.5 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-6 py-3 text-lg'
}
return sizes[props.size]
})
const buttonClasses = computed(() => [
'inline-flex items-center justify-center rounded-md font-medium',
'transition-colors duration-150',
'focus:outline-none focus:ring-2 focus:ring-offset-2',
variantClasses.value,
sizeClasses.value
])
</script>
<template>
<button :class="buttonClasses">
<slot />
</button>
</template>
对于复杂的组件变体,使用带有辅助库的 CVA 模式:
正确:CVA 风格的变体管理
<script setup lang="ts">
import { computed } from 'vue'
import { cva, type VariantProps } from 'class-variance-authority'
const button = cva(
'inline-flex items-center justify-center rounded-md font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2',
{
variants: {
intent: {
primary: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500',
secondary: 'bg-gray-200 text-gray-900 hover:bg-gray-300 focus:ring-gray-500',
danger: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500'
},
size: {
sm: 'px-3 py-1.5 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-6 py-3 text-lg'
}
},
defaultVariants: {
intent: 'primary',
size: 'md'
}
}
)
type ButtonProps = VariantProps<typeof button>
const props = defineProps<{
intent?: ButtonProps['intent']
size?: ButtonProps['size']
}>()
const classes = computed(() => button({ intent: props.intent, size: props.size }))
</script>
<template>
<button :class="classes">
<slot />
</button>
</template>
将重复的实用模式提取到 Vue 组件中:
正确:可重用的卡片组件
<!-- components/BaseCard.vue -->
<script setup lang="ts">
withDefaults(defineProps<{
padding?: 'none' | 'sm' | 'md' | 'lg'
shadow?: 'none' | 'sm' | 'md' | 'lg'
}>(), {
padding: 'md',
shadow: 'md'
})
</script>
<template>
<div
class="rounded-xl bg-white dark:bg-gray-800"
:class="[
{
'p-0': padding === 'none',
'p-4': padding === 'sm',
'p-6': padding === 'md',
'p-8': padding === 'lg'
},
{
'shadow-none': shadow === 'none',
'shadow-sm': shadow === 'sm',
'shadow-md': shadow === 'md',
'shadow-lg': shadow === 'lg'
}
]"
>
<slot />
</div>
</template>
在 Tailwind 配置中定义设计令牌以确保一致性:
正确:带设计令牌的 tailwind.config.js
/** @type {import('tailwindcss').Config} */
export default {
content: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],
theme: {
extend: {
colors: {
// 语义化颜色令牌
primary: {
50: '#eff6ff',
100: '#dbeafe',
500: '#3b82f6',
600: '#2563eb',
700: '#1d4ed8'
},
surface: {
light: '#ffffff',
dark: '#1f2937'
}
},
spacing: {
// 自定义间距令牌
'4.5': '1.125rem',
'18': '4.5rem'
},
fontFamily: {
sans: ['Inter', 'system-ui', 'sans-serif']
},
borderRadius: {
'4xl': '2rem'
}
}
},
plugins: []
}
对于 Tailwind CSS v4,使用 CSS 优先的配置方法:
正确:Tailwind v4 CSS 配置
/* main.css */
@import "tailwindcss";
@theme {
/* 自定义颜色 */
--color-primary-500: #3b82f6;
--color-primary-600: #2563eb;
--color-primary-700: #1d4ed8;
/* 自定义间距 */
--spacing-4-5: 1.125rem;
--spacing-18: 4.5rem;
/* 自定义字体 */
--font-family-sans: 'Inter', system-ui, sans-serif;
}
cn() 辅助函数处理条件类使用类合并工具处理条件类:
正确:结合 clsx 和 tailwind-merge 的 cn() 辅助函数
// utils/cn.ts
import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
在组件中的使用:
<script setup lang="ts">
import { cn } from '@/utils/cn'
const props = defineProps<{
class?: string
isActive?: boolean
}>()
</script>
<template>
<div
:class="cn(
'rounded-lg border p-4 transition-colors',
isActive ? 'border-blue-500 bg-blue-50' : 'border-gray-200 bg-white',
props.class
)"
>
<slot />
</div>
</template>
PrimeVue 是一个全面的 Vue UI 组件库,包含 90 多个组件。遵循这些模式以实现有效的集成和自定义。
正确:使用 Vue 3 的 PrimeVue v4 设置
// main.ts
import { createApp } from 'vue'
import PrimeVue from 'primevue/config'
import Aura from '@primevue/themes/aura'
import App from './App.vue'
const app = createApp(App)
app.use(PrimeVue, {
theme: {
preset: Aura,
options: {
darkModeSelector: '.dark-mode'
}
}
})
app.mount('#app')
正确:组件注册(支持摇树优化)
// main.ts - 仅注册你使用的组件
import Button from 'primevue/button'
import DataTable from 'primevue/datatable'
import Column from 'primevue/column'
app.component('Button', Button)
app.component('DataTable', DataTable)
app.component('Column', Column)
PassThrough API 允许自定义内部 DOM 元素,而无需修改组件源代码:
正确:组件级别的 PassThrough
<script setup lang="ts">
import Panel from 'primevue/panel'
</script>
<template>
<Panel
header="用户资料"
toggleable
:pt="{
header: {
class: 'bg-primary-100 dark:bg-primary-900'
},
content: {
class: 'p-6'
},
title: {
class: 'text-xl font-semibold'
},
toggler: {
class: 'hover:bg-primary-200 dark:hover:bg-primary-800 rounded-full'
}
}"
>
<p>面板内容在此</p>
</Panel>
</template>
正确:带状态的动态 PassThrough
<script setup lang="ts">
import Panel from 'primevue/panel'
</script>
<template>
<Panel
header="可折叠面板"
toggleable
:pt="{
header: (options) => ({
class: [
'transition-colors duration-200',
{
'bg-primary-500 text-white': options.state.d_collapsed,
'bg-surface-100 dark:bg-surface-800': !options.state.d_collapsed
}
]
})
}"
>
<p>内容在折叠时改变标题样式</p>
</Panel>
</template>
在应用程序级别定义共享样式:
正确:全局 PT 配置
// main.ts
import PrimeVue from 'primevue/config'
import Aura from '@primevue/themes/aura'
app.use(PrimeVue, {
theme: {
preset: Aura
},
pt: {
// 所有按钮获得一致的样式
button: {
root: {
class: 'rounded-lg font-medium transition-all duration-200'
}
},
// 所有输入框获得一致的样式
inputtext: {
root: {
class: 'rounded-lg border-2 focus:ring-2 focus:ring-primary-500'
}
},
// 所有面板共享样式
panel: {
header: {
class: 'bg-surface-50 dark:bg-surface-900'
}
},
// 全局 CSS 注入
global: {
css: `
.p-component {
font-family: 'Inter', sans-serif;
}
`
}
}
})
使用自定义修改扩展现有预设:
正确:扩展 Tailwind 预设
// presets/custom-tailwind.ts
import { usePassThrough } from 'primevue/passthrough'
import Tailwind from 'primevue/passthrough/tailwind'
export const CustomTailwind = usePassThrough(
Tailwind,
{
panel: {
header: {
class: ['bg-gradient-to-r from-primary-500 to-primary-600']
},
title: {
class: ['text-white font-bold']
}
},
button: {
root: {
class: ['shadow-lg hover:shadow-xl transition-shadow']
}
}
},
{
mergeSections: true, // 保留原始部分
mergeProps: false // 替换 props(不合并数组)
}
)
合并策略参考:
| mergeSections | mergeProps | 行为 |
|---|---|---|
true | false | 自定义值替换原始值(默认) |
true | true | 自定义值与原始值合并 |
false | true | 仅包含自定义部分 |
false | false | 最小化 - 仅自定义部分,不合并 |
使用无样式的 PrimeVue 组件,获得完整的 Tailwind 控制:
正确:无样式模式配置
// main.ts
import PrimeVue from 'primevue/config'
app.use(PrimeVue, {
unstyled: true // 移除所有默认样式
})
正确:使用无样式模式的自定义样式按钮
<script setup lang="ts">
import Button from 'primevue/button'
</script>
<template>
<Button
label="提交"
:pt="{
root: {
class: [
'inline-flex items-center justify-center',
'px-4 py-2 rounded-lg font-medium',
'bg-primary-600 text-white',
'hover:bg-primary-700 active:bg-primary-800',
'focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2',
'transition-colors duration-150',
'disabled:opacity-50 disabled:cursor-not-allowed'
]
},
label: {
class: 'font-medium'
},
icon: {
class: 'mr-2'
}
}"
:ptOptions="{ mergeSections: false, mergeProps: false }"
/>
</template>
创建可重用的包装组件以实现一致的样式:
正确:按钮包装组件
<!-- components/ui/AppButton.vue -->
<script setup lang="ts">
import Button from 'primevue/button'
type ButtonVariant =
Comprehensive best practices guide for Vue.js 3 applications. Contains guidelines across multiple categories to ensure idiomatic, maintainable, and scalable Vue.js code, including Tailwind CSS integration patterns for utility-first styling and PrimeVue component library best practices.
Reference these guidelines when:
| Category | Focus | Prefix |
|---|---|---|
| Composition API | Proper use of Composition API patterns | composition- |
| Component Design | Component structure and organization | component- |
| Reactivity | Reactive state management patterns | reactive- |
| Props & Events | Component communication patterns | props- |
| Template Patterns | Template syntax best practices | template- |
| Code Organization | Project and code structure | organization- |
| TypeScript | Type-safe Vue.js patterns | typescript- |
| Error Handling | Error boundaries and handling | error- |
| Tailwind CSS | Utility-first styling patterns | tailwind- |
| PrimeVue | Component library integration patterns | primevue- |
composition-script-setup - Always use <script setup> for single-file componentscomposition-ref-vs-reactive - Use ref() for primitives, reactive() for objectscomposition-computed-derived - Use computed() for all derived statecomposition-watch-side-effects - Use watch()/watchEffect() only for side effectscomponent-single-responsibility - One component, one purposecomponent-naming-convention - Use PascalCase for components, kebab-case in templatescomponent-small-focused - Keep components under 200 linescomponent-presentational-container - Separate logic from presentation when beneficialcomponent-slots-flexibility - Use slots for flexible component compositioncomponent-expose-minimal - Only expose what's necessary via defineExpose()reactive-const-refs - Always declare refs with constreactive-unwrap-template - Let Vue unwrap refs in templates (no .value)reactive-shallow-large-data - Use shallowRef()/shallowReactive() for large non-reactive datareactive-readonly-props - Use readonly() to prevent mutationsreactive-toRefs-destructure - Use when destructuring reactive objectsprops-define-types - Always define prop types with defineProps<T>()props-required-explicit - Be explicit about required vs optional propsprops-default-values - Provide sensible defaults with withDefaults()props-immutable - Never mutate props directlyprops-validation - Use validator functions for complex prop validationevents-define-emits - Always define emits with defineEmits<T>()events-naming - Use kebab-case for event names in templatestemplate-v-if-v-show - Use v-if for conditional rendering, v-show for togglingtemplate-v-for-key - Always use unique, stable :key with v-fortemplate-v-if-v-for - Never use v-if and v-for on the same elementtemplate-computed-expressions - Move complex expressions to computed propertiestemplate-event-modifiers - Use event modifiers (, ) appropriatelyorganization-feature-folders - Organize by feature, not by typeorganization-composables-folder - Keep composables in dedicated composables/ folderorganization-barrel-exports - Use index files for clean importsorganization-consistent-naming - Follow consistent naming conventionsorganization-colocation - Colocate related files (component, tests, styles)typescript-generic-components - Use generics for reusable typed componentstypescript-prop-types - Use TypeScript interfaces for prop definitionstypescript-emit-types - Type emit payloads explicitlytypescript-ref-typing - Specify types for refs when not inferredtypescript-template-refs - Type template refs with ref<InstanceType<typeof Component> | null>(null)error-boundaries - Use onErrorCaptured() for component error boundarieserror-async-handling - Handle errors in async operations explicitlyerror-provide-fallbacks - Provide fallback UI for error stateserror-logging - Log errors appropriately for debuggingtailwind-utility-first - Apply utility classes directly in templates, avoid custom CSStailwind-class-order - Use consistent class ordering (layout → spacing → typography → visual)tailwind-responsive-mobile-first - Use mobile-first responsive design (sm:, md:, lg:)tailwind-component-extraction - Extract repeated utility patterns into Vue componentstailwind-dynamic-classes - Use computed properties or helper functions for dynamic classestailwind-complete-class-strings - Always use complete class strings, never concatenateprimevue-design-tokens - Use design tokens over CSS overrides for themingprimevue-passthrough-api - Use PassThrough (pt) API for component customizationprimevue-wrapper-components - Wrap PrimeVue components for consistent styling across appsprimevue-unstyled-mode - Use unstyled mode with Tailwind for full styling controlprimevue-global-pt-config - Define shared PassThrough properties at app levelprimevue-merge-strategies - Choose appropriate merge strategies for PT customizationprimevue-use-passthrough-utility - Use usePassThrough for extending presetsprimevue-typed-components - Leverage PrimeVue's TypeScript support for type safetyThe Composition API is the recommended approach for Vue.js 3. Follow these patterns:
<script setup>: More concise, better TypeScript inference, and improved performanceuse prefix convention (e.g., useAuth, useFetch)Well-designed components are the foundation of maintainable Vue applications:
Understanding Vue's reactivity system is crucial:
ref() for primitives and values you'll reassign; use reactive() for objects you'll mutatecomputed() insteadwatch() for side effects like API calls or localStoragetoRefs()Proper component communication ensures maintainable code:
defineProps<T>()defineEmits<T>() for type-safe event handlingmodelValue prop and update:modelValue emitRecommended structure for<script setup>:
<script setup lang="ts">
// 1. Imports
import { ref, computed, watch, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import type { User } from '@/types'
// 2. Props and Emits
const props = defineProps<{
userId: string
initialData?: User
}>()
const emit = defineEmits<{
submit: [user: User]
cancel: []
}>()
// 3. Composables
const router = useRouter()
const { user, loading, error } = useUser(props.userId)
// 4. Reactive State
const formData = ref({ name: '', email: '' })
const isEditing = ref(false)
// 5. Computed Properties
const isValid = computed(() => {
return formData.value.name.length > 0 && formData.value.email.includes('@')
})
// 6. Watchers (for side effects only)
watch(() => props.userId, (newId) => {
fetchUserData(newId)
})
// 7. Methods
function handleSubmit() {
if (isValid.value) {
emit('submit', formData.value)
}
}
// 8. Lifecycle Hooks
onMounted(() => {
if (props.initialData) {
formData.value = { ...props.initialData }
}
})
</script>
Correct: Well-structured composable
// composables/useUser.ts
import { ref, computed, watch } from 'vue'
import type { Ref } from 'vue'
import type { User } from '@/types'
export function useUser(userId: Ref<string> | string) {
// State
const user = ref<User | null>(null)
const loading = ref(false)
const error = ref<Error | null>(null)
// Computed
const fullName = computed(() => {
if (!user.value) return ''
return `${user.value.firstName} ${user.value.lastName}`
})
// Methods
async function fetchUser(id: string) {
loading.value = true
error.value = null
try {
const response = await api.getUser(id)
user.value = response.data
} catch (e) {
error.value = e as Error
} finally {
loading.value = false
}
}
// Auto-fetch when userId changes (if reactive)
if (isRef(userId)) {
watch(userId, (newId) => fetchUser(newId), { immediate: true })
} else {
fetchUser(userId)
}
// Return
return {
user: readonly(user),
fullName,
loading: readonly(loading),
error: readonly(error),
refresh: () => fetchUser(unref(userId))
}
}
Correct: Typed props with defaults
<script setup lang="ts">
interface Props {
title: string
size?: 'sm' | 'md' | 'lg'
disabled?: boolean
items?: string[]
}
const props = withDefaults(defineProps<Props>(), {
size: 'md',
disabled: false,
items: () => [] // Use factory function for arrays/objects
})
</script>
Correct: Typed emits with payloads
<script setup lang="ts">
interface FormData {
name: string
email: string
}
const emit = defineEmits<{
submit: [data: FormData]
cancel: []
'update:modelValue': [value: string]
}>()
function handleSubmit(data: FormData) {
emit('submit', data)
}
</script>
Correct: Custom v-model with defineModel (Vue 3.4+)
<script setup lang="ts">
const model = defineModel<string>({ required: true })
// Or with default
const modelWithDefault = defineModel<string>({ default: '' })
</script>
<template>
<input :value="model" @input="model = $event.target.value" />
</template>
Correct: Custom v-model (Vue 3.3 and earlier)
<script setup lang="ts">
const props = defineProps<{
modelValue: string
}>()
const emit = defineEmits<{
'update:modelValue': [value: string]
}>()
const value = computed({
get: () => props.modelValue,
set: (val) => emit('update:modelValue', val)
})
</script>
<template>
<input v-model="value" />
</template>
Correct: Typed template refs
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import MyComponent from './MyComponent.vue'
// DOM element ref
const inputRef = ref<HTMLInputElement | null>(null)
// Component ref
const componentRef = ref<InstanceType<typeof MyComponent> | null>(null)
onMounted(() => {
inputRef.value?.focus()
componentRef.value?.someExposedMethod()
})
</script>
<template>
<input ref="inputRef" />
<MyComponent ref="componentRef" />
</template>
Correct: Type-safe provide/inject
// types/injection-keys.ts
import type { InjectionKey, Ref } from 'vue'
import type { User } from './user'
export const UserKey: InjectionKey<Ref<User>> = Symbol('user')
// Parent component
import { provide, ref } from 'vue'
import { UserKey } from '@/types/injection-keys'
const user = ref<User>({ id: '1', name: 'John' })
provide(UserKey, user)
// Child component
import { inject } from 'vue'
import { UserKey } from '@/types/injection-keys'
const user = inject(UserKey)
if (!user) {
throw new Error('User not provided')
}
Correct: Error boundary with onErrorCaptured
<script setup lang="ts">
import { ref, onErrorCaptured } from 'vue'
const error = ref<Error | null>(null)
onErrorCaptured((err) => {
error.value = err
// Return false to stop error propagation
return false
})
function reset() {
error.value = null
}
</script>
<template>
<div v-if="error" class="error-boundary">
<p>Something went wrong: {{ error.message }}</p>
<button @click="reset">Try again</button>
</div>
<slot v-else />
</template>
Correct: Async components with loading/error states
import { defineAsyncComponent } from 'vue'
const AsyncDashboard = defineAsyncComponent({
loader: () => import('./Dashboard.vue'),
loadingComponent: LoadingSpinner,
errorComponent: ErrorDisplay,
delay: 200, // Show loading after 200ms
timeout: 10000 // Timeout after 10s
})
Vue's component-based architecture pairs naturally with Tailwind's utility-first approach. Follow these patterns for maintainable, consistent styling.
Apply Tailwind utility classes directly in Vue templates for rapid, consistent styling:
Correct: Utility classes in template
<template>
<div class="mx-auto max-w-md rounded-xl bg-white p-6 shadow-lg">
<h2 class="text-xl font-semibold text-gray-900">{{ title }}</h2>
<p class="mt-2 text-gray-600">{{ description }}</p>
<button class="mt-4 rounded-md bg-blue-600 px-4 py-2 text-white hover:bg-blue-700">
{{ buttonText }}
</button>
</div>
</template>
Maintain consistent class ordering for readability. Recommended order:
flex, grid, block, hiddenrelative, absolute, fixedw-, h-, m-, p-Use the official Prettier plugin (prettier-plugin-tailwindcss) to automatically sort classes.
Use Tailwind's responsive prefixes for mobile-first responsive design:
Correct: Mobile-first responsive layout
<template>
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
<article
v-for="item in items"
:key="item.id"
class="p-4 text-sm sm:p-6 sm:text-base lg:text-lg"
>
<h3 class="font-medium">{{ item.title }}</h3>
</article>
</div>
</template>
Breakpoint Reference:
sm: - 640px and upmd: - 768px and uplg: - 1024px and upxl: - 1280px and up2xl: - 1536px and upUse state variants for interactive elements:
Correct: State variants for buttons
<template>
<button
class="rounded-lg bg-indigo-600 px-4 py-2 font-medium text-white
transition-colors duration-150
hover:bg-indigo-700
focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2
active:bg-indigo-800
disabled:cursor-not-allowed disabled:opacity-50"
:disabled="isLoading"
>
{{ isLoading ? 'Loading...' : 'Submit' }}
</button>
</template>
Use the dark: prefix for dark mode styles:
Correct: Dark mode support
<template>
<div class="bg-white dark:bg-gray-900">
<h1 class="text-gray-900 dark:text-white">{{ title }}</h1>
<p class="text-gray-600 dark:text-gray-400">{{ content }}</p>
<div class="border-gray-200 dark:border-gray-700 rounded-lg border p-4">
<slot />
</div>
</div>
</template>
Use computed properties for conditional class binding:
Correct: Computed classes for variants
<script setup lang="ts">
import { computed } from 'vue'
type ButtonVariant = 'primary' | 'secondary' | 'danger'
type ButtonSize = 'sm' | 'md' | 'lg'
const props = withDefaults(defineProps<{
variant?: ButtonVariant
size?: ButtonSize
}>(), {
variant: 'primary',
size: 'md'
})
const variantClasses = computed(() => {
const variants: Record<ButtonVariant, string> = {
primary: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500',
secondary: 'bg-gray-200 text-gray-900 hover:bg-gray-300 focus:ring-gray-500',
danger: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500'
}
return variants[props.variant]
})
const sizeClasses = computed(() => {
const sizes: Record<ButtonSize, string> = {
sm: 'px-3 py-1.5 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-6 py-3 text-lg'
}
return sizes[props.size]
})
const buttonClasses = computed(() => [
'inline-flex items-center justify-center rounded-md font-medium',
'transition-colors duration-150',
'focus:outline-none focus:ring-2 focus:ring-offset-2',
variantClasses.value,
sizeClasses.value
])
</script>
<template>
<button :class="buttonClasses">
<slot />
</button>
</template>
For complex component variants, use the CVA pattern with a helper library:
Correct: CVA-style variant management
<script setup lang="ts">
import { computed } from 'vue'
import { cva, type VariantProps } from 'class-variance-authority'
const button = cva(
'inline-flex items-center justify-center rounded-md font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2',
{
variants: {
intent: {
primary: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500',
secondary: 'bg-gray-200 text-gray-900 hover:bg-gray-300 focus:ring-gray-500',
danger: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500'
},
size: {
sm: 'px-3 py-1.5 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-6 py-3 text-lg'
}
},
defaultVariants: {
intent: 'primary',
size: 'md'
}
}
)
type ButtonProps = VariantProps<typeof button>
const props = defineProps<{
intent?: ButtonProps['intent']
size?: ButtonProps['size']
}>()
const classes = computed(() => button({ intent: props.intent, size: props.size }))
</script>
<template>
<button :class="classes">
<slot />
</button>
</template>
Extract repeated utility patterns into Vue components:
Correct: Reusable card component
<!-- components/BaseCard.vue -->
<script setup lang="ts">
withDefaults(defineProps<{
padding?: 'none' | 'sm' | 'md' | 'lg'
shadow?: 'none' | 'sm' | 'md' | 'lg'
}>(), {
padding: 'md',
shadow: 'md'
})
</script>
<template>
<div
class="rounded-xl bg-white dark:bg-gray-800"
:class="[
{
'p-0': padding === 'none',
'p-4': padding === 'sm',
'p-6': padding === 'md',
'p-8': padding === 'lg'
},
{
'shadow-none': shadow === 'none',
'shadow-sm': shadow === 'sm',
'shadow-md': shadow === 'md',
'shadow-lg': shadow === 'lg'
}
]"
>
<slot />
</div>
</template>
Define design tokens in your Tailwind config for consistency:
Correct: tailwind.config.js with design tokens
/** @type {import('tailwindcss').Config} */
export default {
content: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],
theme: {
extend: {
colors: {
// Semantic color tokens
primary: {
50: '#eff6ff',
100: '#dbeafe',
500: '#3b82f6',
600: '#2563eb',
700: '#1d4ed8'
},
surface: {
light: '#ffffff',
dark: '#1f2937'
}
},
spacing: {
// Custom spacing tokens
'4.5': '1.125rem',
'18': '4.5rem'
},
fontFamily: {
sans: ['Inter', 'system-ui', 'sans-serif']
},
borderRadius: {
'4xl': '2rem'
}
}
},
plugins: []
}
For Tailwind CSS v4, use the CSS-first configuration approach:
Correct: Tailwind v4 CSS configuration
/* main.css */
@import "tailwindcss";
@theme {
/* Custom colors */
--color-primary-500: #3b82f6;
--color-primary-600: #2563eb;
--color-primary-700: #1d4ed8;
/* Custom spacing */
--spacing-4-5: 1.125rem;
--spacing-18: 4.5rem;
/* Custom fonts */
--font-family-sans: 'Inter', system-ui, sans-serif;
}
cn() Helper for Conditional ClassesUse a class merging utility for conditional classes:
Correct: cn() helper with clsx and tailwind-merge
// utils/cn.ts
import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
Usage in component:
<script setup lang="ts">
import { cn } from '@/utils/cn'
const props = defineProps<{
class?: string
isActive?: boolean
}>()
</script>
<template>
<div
:class="cn(
'rounded-lg border p-4 transition-colors',
isActive ? 'border-blue-500 bg-blue-50' : 'border-gray-200 bg-white',
props.class
)"
>
<slot />
</div>
</template>
PrimeVue is a comprehensive Vue UI component library with 90+ components. Follow these patterns for effective integration and customization.
Correct: PrimeVue v4 setup with Vue 3
// main.ts
import { createApp } from 'vue'
import PrimeVue from 'primevue/config'
import Aura from '@primevue/themes/aura'
import App from './App.vue'
const app = createApp(App)
app.use(PrimeVue, {
theme: {
preset: Aura,
options: {
darkModeSelector: '.dark-mode'
}
}
})
app.mount('#app')
Correct: Component registration (tree-shakeable)
// main.ts - Register only components you use
import Button from 'primevue/button'
import DataTable from 'primevue/datatable'
import Column from 'primevue/column'
app.component('Button', Button)
app.component('DataTable', DataTable)
app.component('Column', Column)
The PassThrough API allows customization of internal DOM elements without modifying component source:
Correct: Component-level PassThrough
<script setup lang="ts">
import Panel from 'primevue/panel'
</script>
<template>
<Panel
header="User Profile"
toggleable
:pt="{
header: {
class: 'bg-primary-100 dark:bg-primary-900'
},
content: {
class: 'p-6'
},
title: {
class: 'text-xl font-semibold'
},
toggler: {
class: 'hover:bg-primary-200 dark:hover:bg-primary-800 rounded-full'
}
}"
>
<p>Panel content here</p>
</Panel>
</template>
Correct: Dynamic PassThrough with state
<script setup lang="ts">
import Panel from 'primevue/panel'
</script>
<template>
<Panel
header="Collapsible Panel"
toggleable
:pt="{
header: (options) => ({
class: [
'transition-colors duration-200',
{
'bg-primary-500 text-white': options.state.d_collapsed,
'bg-surface-100 dark:bg-surface-800': !options.state.d_collapsed
}
]
})
}"
>
<p>Content changes header style when collapsed</p>
</Panel>
</template>
Define shared styles at the application level:
Correct: Global PT configuration
// main.ts
import PrimeVue from 'primevue/config'
import Aura from '@primevue/themes/aura'
app.use(PrimeVue, {
theme: {
preset: Aura
},
pt: {
// All buttons get consistent styling
button: {
root: {
class: 'rounded-lg font-medium transition-all duration-200'
}
},
// All inputs get consistent styling
inputtext: {
root: {
class: 'rounded-lg border-2 focus:ring-2 focus:ring-primary-500'
}
},
// All panels share styling
panel: {
header: {
class: 'bg-surface-50 dark:bg-surface-900'
}
},
// Global CSS injection
global: {
css: `
.p-component {
font-family: 'Inter', sans-serif;
}
`
}
}
})
Extend existing presets with custom modifications:
Correct: Extending Tailwind preset
// presets/custom-tailwind.ts
import { usePassThrough } from 'primevue/passthrough'
import Tailwind from 'primevue/passthrough/tailwind'
export const CustomTailwind = usePassThrough(
Tailwind,
{
panel: {
header: {
class: ['bg-gradient-to-r from-primary-500 to-primary-600']
},
title: {
class: ['text-white font-bold']
}
},
button: {
root: {
class: ['shadow-lg hover:shadow-xl transition-shadow']
}
}
},
{
mergeSections: true, // Keep original sections
mergeProps: false // Replace props (don't merge arrays)
}
)
Merge Strategy Reference:
| mergeSections | mergeProps | Behavior |
|---|---|---|
true | false | Custom value replaces original (default) |
true | true | Custom values merge with original |
false | true | Only custom sections included |
false |
Use unstyled PrimeVue components with full Tailwind control:
Correct: Unstyled mode configuration
// main.ts
import PrimeVue from 'primevue/config'
app.use(PrimeVue, {
unstyled: true // Remove all default styles
})
Correct: Custom styled button with unstyled mode
<script setup lang="ts">
import Button from 'primevue/button'
</script>
<template>
<Button
label="Submit"
:pt="{
root: {
class: [
'inline-flex items-center justify-center',
'px-4 py-2 rounded-lg font-medium',
'bg-primary-600 text-white',
'hover:bg-primary-700 active:bg-primary-800',
'focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2',
'transition-colors duration-150',
'disabled:opacity-50 disabled:cursor-not-allowed'
]
},
label: {
class: 'font-medium'
},
icon: {
class: 'mr-2'
}
}"
:ptOptions="{ mergeSections: false, mergeProps: false }"
/>
</template>
Create reusable wrapper components for consistent styling:
Correct: Button wrapper component
<!-- components/ui/AppButton.vue -->
<script setup lang="ts">
import Button from 'primevue/button'
type ButtonVariant = 'primary' | 'secondary' | 'danger' | 'ghost'
type ButtonSize = 'sm' | 'md' | 'lg'
const props = withDefaults(defineProps<{
variant?: ButtonVariant
size?: ButtonSize
loading?: boolean
}>(), {
variant: 'primary',
size: 'md',
loading: false
})
const variantClasses: Record<ButtonVariant, string> = {
primary: 'bg-primary-600 text-white hover:bg-primary-700 focus:ring-primary-500',
secondary: 'bg-surface-200 text-surface-900 hover:bg-surface-300 focus:ring-surface-500',
danger: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500',
ghost: 'bg-transparent text-primary-600 hover:bg-primary-50 focus:ring-primary-500'
}
const sizeClasses: Record<ButtonSize, string> = {
sm: 'px-3 py-1.5 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-6 py-3 text-lg'
}
</script>
<template>
<Button
v-bind="$attrs"
:loading="loading"
:pt="{
root: {
class: [
'inline-flex items-center justify-center rounded-lg font-medium',
'transition-all duration-200',
'focus:outline-none focus:ring-2 focus:ring-offset-2',
'disabled:opacity-50 disabled:cursor-not-allowed',
variantClasses[variant],
sizeClasses[size]
]
}
}"
:ptOptions="{ mergeSections: false, mergeProps: false }"
>
<slot />
</Button>
</template>
<script lang="ts">
export default {
inheritAttrs: false
}
</script>
Usage:
<template>
<AppButton variant="primary" size="lg" @click="handleSubmit">
Submit Form
</AppButton>
<AppButton variant="ghost" size="sm">
Cancel
</AppButton>
</template>
Correct: Typed DataTable with Composition API
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import DataTable from 'primevue/datatable'
import Column from 'primevue/column'
interface User {
id: number
name: string
email: string
role: string
status: 'active' | 'inactive'
}
const users = ref<User[]>([])
const loading = ref(true)
const selectedUsers = ref<User[]>([])
// Pagination
const first = ref(0)
const rows = ref(10)
// Sorting
const sortField = ref<string>('name')
const sortOrder = ref<1 | -1>(1)
onMounted(async () => {
loading.value = true
try {
users.value = await fetchUsers()
} finally {
loading.value = false
}
})
</script>
<template>
<DataTable
v-model:selection="selectedUsers"
:value="users"
:loading="loading"
:paginator="true"
:rows="rows"
:first="first"
:sortField="sortField"
:sortOrder="sortOrder"
dataKey="id"
stripedRows
removableSort
@page="(e) => first = e.first"
@sort="(e) => { sortField = e.sortField; sortOrder = e.sortOrder }"
>
<Column selectionMode="multiple" headerStyle="width: 3rem" />
<Column field="name" header="Name" sortable />
<Column field="email" header="Email" sortable />
<Column field="role" header="Role" sortable />
<Column field="status" header="Status">
<template #body="{ data }">
<span
:class="[
'px-2 py-1 rounded-full text-xs font-medium',
data.status === 'active'
? 'bg-green-100 text-green-800'
: 'bg-red-100 text-red-800'
]"
>
{{ data.status }}
</span>
</template>
</Column>
</DataTable>
</template>
Correct: Form with validation using PrimeVue
<script setup lang="ts">
import { ref, computed } from 'vue'
import InputText from 'primevue/inputtext'
import Password from 'primevue/password'
import Dropdown from 'primevue/dropdown'
import Button from 'primevue/button'
import Message from 'primevue/message'
interface FormData {
email: string
password: string
role: string | null
}
const formData = ref<FormData>({
email: '',
password: '',
role: null
})
const errors = ref<Partial<Record<keyof FormData, string>>>({})
const submitted = ref(false)
const roles = [
{ label: 'Admin', value: 'admin' },
{ label: 'User', value: 'user' },
{ label: 'Guest', value: 'guest' }
]
const isValid = computed(() => {
return Object.keys(errors.value).length === 0
})
function validate(): boolean {
errors.value = {}
if (!formData.value.email) {
errors.value.email = 'Email is required'
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.value.email)) {
errors.value.email = 'Invalid email format'
}
if (!formData.value.password) {
errors.value.password = 'Password is required'
} else if (formData.value.password.length < 8) {
errors.value.password = 'Password must be at least 8 characters'
}
if (!formData.value.role) {
errors.value.role = 'Role is required'
}
return Object.keys(errors.value).length === 0
}
function handleSubmit() {
submitted.value = true
if (validate()) {
// Submit form
console.log('Form submitted:', formData.value)
}
}
</script>
<template>
<form @submit.prevent="handleSubmit" class="space-y-4">
<div class="flex flex-col gap-2">
<label for="email" class="font-medium">Email</label>
<InputText
id="email"
v-model="formData.email"
:class="{ 'p-invalid': errors.email }"
aria-describedby="email-error"
/>
<Message v-if="errors.email" severity="error" :closable="false">
{{ errors.email }}
</Message>
</div>
<div class="flex flex-col gap-2">
<label for="password" class="font-medium">Password</label>
<Password
id="password"
v-model="formData.password"
:class="{ 'p-invalid': errors.password }"
toggleMask
:feedback="false"
aria-describedby="password-error"
/>
<Message v-if="errors.password" severity="error" :closable="false">
{{ errors.password }}
</Message>
</div>
<div class="flex flex-col gap-2">
<label for="role" class="font-medium">Role</label>
<Dropdown
id="role"
v-model="formData.role"
:options="roles"
optionLabel="label"
optionValue="value"
placeholder="Select a role"
:class="{ 'p-invalid': errors.role }"
aria-describedby="role-error"
/>
<Message v-if="errors.role" severity="error" :closable="false">
{{ errors.role }}
</Message>
</div>
<Button type="submit" label="Submit" class="w-full" />
</form>
</template>
Correct: Confirmation dialog with composable
// composables/useConfirmDialog.ts
import { useConfirm } from 'primevue/useconfirm'
export function useConfirmDialog() {
const confirm = useConfirm()
function confirmDelete(
message: string,
onAccept: () => void,
onReject?: () => void
) {
confirm.require({
message,
header: 'Confirm Delete',
icon: 'pi pi-exclamation-triangle',
rejectClass: 'p-button-secondary p-button-outlined',
acceptClass: 'p-button-danger',
rejectLabel: 'Cancel',
acceptLabel: 'Delete',
accept: onAccept,
reject: onReject
})
}
function confirmAction(options: {
message: string
header: string
onAccept: () => void
onReject?: () => void
}) {
confirm.require({
message: options.message,
header: options.header,
icon: 'pi pi-info-circle',
rejectClass: 'p-button-secondary p-button-outlined',
acceptClass: 'p-button-primary',
accept: options.onAccept,
reject: options.onReject
})
}
return {
confirmDelete,
confirmAction
}
}
Usage:
<script setup lang="ts">
import { useConfirmDialog } from '@/composables/useConfirmDialog'
import ConfirmDialog from 'primevue/confirmdialog'
const { confirmDelete } = useConfirmDialog()
function handleDelete(item: Item) {
confirmDelete(
`Are you sure you want to delete "${item.name}"?`,
() => deleteItem(item.id)
)
}
</script>
<template>
<ConfirmDialog />
<Button label="Delete" severity="danger" @click="handleDelete(item)" />
</template>
Correct: Toast service with composable
// composables/useNotifications.ts
import { useToast } from 'primevue/usetoast'
export function useNotifications() {
const toast = useToast()
function success(summary: string, detail?: string) {
toast.add({
severity: 'success',
summary,
detail,
life: 3000
})
}
function error(summary: string, detail?: string) {
toast.add({
severity: 'error',
summary,
detail,
life: 5000
})
}
function warn(summary: string, detail?: string) {
toast.add({
severity: 'warn',
summary,
detail,
life: 4000
})
}
function info(summary: string, detail?: string) {
toast.add({
severity: 'info',
summary,
detail,
life: 3000
})
}
return { success, error, warn, info }
}
PrimeVue components are WCAG 2.0 compliant. Ensure proper usage:
Correct: Accessible form fields
<template>
<div class="flex flex-col gap-2">
<label :for="id" class="font-medium">
{{ label }}
<span v-if="required" class="text-red-500" aria-hidden="true">*</span>
</label>
<InputText
:id="id"
v-model="modelValue"
:aria-required="required"
:aria-invalid="!!error"
:aria-describedby="error ? `${id}-error` : undefined"
/>
<small
v-if="error"
:id="`${id}-error`"
class="text-red-500"
role="alert"
>
{{ error }}
</small>
</div>
</template>
Correct: Async component loading for large PrimeVue components
// components/lazy/index.ts
import { defineAsyncComponent } from 'vue'
export const LazyDataTable = defineAsyncComponent({
loader: () => import('primevue/datatable'),
loadingComponent: () => import('@/components/ui/TableSkeleton.vue'),
delay: 200
})
export const LazyEditor = defineAsyncComponent({
loader: () => import('primevue/editor'),
loadingComponent: () => import('@/components/ui/EditorSkeleton.vue'),
delay: 200
})
export const LazyChart = defineAsyncComponent({
loader: () => import('primevue/chart'),
loadingComponent: () => import('@/components/ui/ChartSkeleton.vue'),
delay: 200
})
Incorrect:
<script setup>
const props = defineProps(['items'])
function addItem(item) {
props.items.push(item) // Never mutate props!
}
</script>
Correct:
<script setup>
const props = defineProps(['items'])
const emit = defineEmits(['update:items'])
function addItem(item) {
emit('update:items', [...props.items, item])
}
</script>
Incorrect:
<template>
<div v-for="item in items" v-if="item.isActive" :key="item.id">
{{ item.name }}
</div>
</template>
Correct:
<script setup>
const activeItems = computed(() => items.value.filter(item => item.isActive))
</script>
<template>
<div v-for="item in activeItems" :key="item.id">
{{ item.name }}
</div>
</template>
Incorrect:
<script setup>
const items = ref([])
const itemCount = ref(0) // Derived state stored separately
watch(items, () => {
itemCount.value = items.value.length // Manually syncing
})
</script>
Correct:
<script setup>
const items = ref([])
const itemCount = computed(() => items.value.length) // Computed property
</script>
Incorrect:
<script setup>
const state = reactive({ count: 0, name: 'Vue' })
const { count, name } = state // Loses reactivity!
</script>
Correct:
<script setup>
const state = reactive({ count: 0, name: 'Vue' })
const { count, name } = toRefs(state) // Preserves reactivity
</script>
Dynamic class concatenation breaks Tailwind's compiler and classes get purged in production:
Incorrect:
<script setup>
const color = ref('blue')
</script>
<template>
<!-- Classes will be purged in production! -->
<div :class="`bg-${color}-500 text-${color}-900`">
Content
</div>
</template>
Correct:
<script setup>
const color = ref<'blue' | 'green' | 'red'>('blue')
const colorClasses = computed(() => {
const colors = {
blue: 'bg-blue-500 text-blue-900',
green: 'bg-green-500 text-green-900',
red: 'bg-red-500 text-red-900'
}
return colors[color.value]
})
</script>
<template>
<div :class="colorClasses">
Content
</div>
</template>
Excessive @apply usage defeats the purpose of utility-first CSS:
Incorrect:
/* styles.css */
.card {
@apply mx-auto max-w-md rounded-xl bg-white p-6 shadow-lg;
}
.card-title {
@apply text-xl font-semibold text-gray-900;
}
.card-description {
@apply mt-2 text-gray-600;
}
.card-button {
@apply mt-4 rounded-md bg-blue-600 px-4 py-2 text-white hover:bg-blue-700;
}
Correct: Use Vue components instead
<!-- components/Card.vue -->
<template>
<div class="mx-auto max-w-md rounded-xl bg-white p-6 shadow-lg">
<h2 class="text-xl font-semibold text-gray-900">
<slot name="title" />
</h2>
<p class="mt-2 text-gray-600">
<slot name="description" />
</p>
<div class="mt-4">
<slot name="actions" />
</div>
</div>
</template>
Applying multiple utilities that target the same CSS property causes unpredictable results:
Incorrect:
<template>
<!-- Both flex and grid target display property -->
<div class="flex grid">Content</div>
<!-- Multiple margin utilities conflict -->
<div class="m-4 mx-6">Content</div>
</template>
Correct:
<template>
<div :class="isGrid ? 'grid' : 'flex'">Content</div>
<!-- Use specific margin utilities -->
<div class="mx-6 my-4">Content</div>
</template>
Always include proper accessibility attributes alongside visual styling:
Incorrect:
<template>
<button class="rounded bg-blue-600 p-2 text-white">
<IconX />
</button>
</template>
Correct:
<template>
<button
class="rounded bg-blue-600 p-2 text-white focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2"
aria-label="Close dialog"
>
<IconX aria-hidden="true" />
</button>
</template>
Break down complex class combinations into logical groups or components:
Incorrect:
<template>
<div class="mx-auto mt-8 flex max-w-4xl flex-col items-center justify-between gap-4 rounded-xl border border-gray-200 bg-white p-6 shadow-lg transition-all duration-300 hover:border-blue-500 hover:shadow-xl dark:border-gray-700 dark:bg-gray-800 sm:flex-row sm:gap-6 md:p-8 lg:gap-8">
<!-- 15+ utilities on one element -->
</div>
</template>
Correct: Extract to component or use computed
<script setup>
const containerClasses = [
// Layout
'mx-auto max-w-4xl flex flex-col sm:flex-row',
'items-center justify-between',
'gap-4 sm:gap-6 lg:gap-8',
// Spacing
'mt-8 p-6 md:p-8',
// Visual
'rounded-xl border bg-white shadow-lg',
'border-gray-200 dark:border-gray-700 dark:bg-gray-800',
// Interactive
'transition-all duration-300',
'hover:border-blue-500 hover:shadow-xl'
]
</script>
<template>
<div :class="containerClasses">
<slot />
</div>
</template>
Using CSS overrides bypasses the design system and causes maintenance issues:
Incorrect:
/* styles.css - Avoid this approach */
.p-button {
background-color: #3b82f6 !important;
border-radius: 8px !important;
}
.p-datatable .p-datatable-thead > tr > th {
background: #f3f4f6 !important;
}
Correct: Use design tokens or PassThrough
// main.ts - Use design tokens
app.use(PrimeVue, {
theme: {
preset: Aura,
options: {
cssLayer: {
name: 'primevue',
order: 'tailwind-base, primevue, tailwind-utilities'
}
}
},
pt: {
button: {
root: { class: 'rounded-lg' }
}
}
})
Importing everything bloats bundle size:
Incorrect:
// main.ts - Don't do this
import PrimeVue from 'primevue/config'
import * as PrimeVueComponents from 'primevue' // Imports everything!
Object.entries(PrimeVueComponents).forEach(([name, component]) => {
app.component(name, component)
})
Correct: Import only what you need
// main.ts - Tree-shakeable imports
import Button from 'primevue/button'
import DataTable from 'primevue/datatable'
import Column from 'primevue/column'
app.component('Button', Button)
app.component('DataTable', DataTable)
app.component('Column', Column)
Mixing modes creates visual inconsistency:
Incorrect:
// main.ts
app.use(PrimeVue, {
unstyled: true // Global unstyled
})
// SomeComponent.vue - Using styled component anyway
<Button label="Click" /> // No styles applied, looks broken
Correct: Choose one approach consistently
// Option 1: Styled mode with PT customization
app.use(PrimeVue, {
theme: { preset: Aura },
pt: { /* global customizations */ }
})
// Option 2: Unstyled mode with complete PT styling
app.use(PrimeVue, {
unstyled: true,
pt: {
button: {
root: { class: 'px-4 py-2 bg-primary-600 text-white rounded-lg' }
}
// ... complete styling for all components
}
})
PrimeVue provides accessibility out of the box, don't disable or ignore it:
Incorrect:
<template>
<!-- Missing aria attributes and label -->
<Button icon="pi pi-trash" @click="deleteItem" />
<!-- No error message association -->
<InputText v-model="email" :class="{ 'p-invalid': hasError }" />
<span class="error">Invalid email</span>
</template>
Correct: Maintain accessibility
<template>
<Button
icon="pi pi-trash"
aria-label="Delete item"
@click="deleteItem"
/>
<div class="flex flex-col gap-2">
<label for="email">Email</label>
<InputText
id="email"
v-model="email"
:class="{ 'p-invalid': hasError }"
:aria-invalid="hasError"
aria-describedby="email-error"
/>
<small id="email-error" v-if="hasError" class="text-red-500" role="alert">
Invalid email
</small>
</div>
</template>
Repeating PT configuration across components creates duplication:
Incorrect:
<!-- ComponentA.vue -->
<Button :pt="{ root: { class: 'rounded-lg shadow-md' } }" />
<!-- ComponentB.vue -->
<Button :pt="{ root: { class: 'rounded-lg shadow-md' } }" />
<!-- ComponentC.vue -->
<Button :pt="{ root: { class: 'rounded-lg shadow-md' } }" />
Correct: Use global PT or wrapper components
// main.ts - Global configuration
app.use(PrimeVue, {
pt: {
button: {
root: { class: 'rounded-lg shadow-md' }
}
}
})
// Or use wrapper components (see Wrapper Components Pattern above)
When using Nuxt.js, follow these additional patterns:
server/api/ for API endpointsuseRuntimeConfig() for environment variablesWeekly Installs
133
Repository
GitHub Stars
2
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode113
github-copilot112
codex108
cursor108
gemini-cli106
claude-code99
Perl安全编程指南:输入验证、注入防护与安全编码实践
1,000 周安装
Git Commit 自动化工具:智能分析代码变更,生成规范提交信息,提升开发效率
105 周安装
PostgreSQL/MySQL数据库模式设计指南:规范化、约束与最佳实践
105 周安装
Helius Solana开发指南:RPC API、实时数据流与区块链工具集成
105 周安装
LLM评估指南:自动化指标、人工评估与A/B测试全面解析
105 周安装
Minecraft Bukkit Pro 插件开发指南:精通Bukkit/Spigot/Paper API与性能优化
105 周安装
Rust异步编程模式指南:基于Tokio的任务、通道、流与错误处理最佳实践
105 周安装
composition-composables - Extract reusable logic into composablescomposition-lifecycle-order - Place lifecycle hooks after reactive state declarationscomposition-avoid-this - Never use this in Composition APItoRefs()reactive-avoid-mutation - Prefer immutable updates for complex stateevents-payload-objects - Pass objects for events with multiple values.prevent.stoptemplate-v-bind-shorthand - Use shorthand syntax (: for v-bind, @ for v-on)template-v-model-modifiers - Use v-model modifiers (.trim, .number, .lazy)tailwind-state-variants - Use state variants (hover:, focus:, active:) for interactionstailwind-dark-mode - Use dark: prefix for dark mode supporttailwind-design-tokens - Configure design tokens in Tailwind config for consistencytailwind-avoid-apply-overuse - Limit @apply usage; prefer Vue components for abstractionprimevue-accessibility - Maintain WCAG compliance with proper aria attributesprimevue-lazy-loading - Use async components for large PrimeVue importstext-, font-, leading-bg-, border-, rounded-, shadow-hover:, focus:, active:false |
| Minimal - only custom sections, no merging |