- API 路由统一: 移除 /api/h5/,统一 /api/pms/ 入口 - 二进制改名: api-server -> pms-api(与服务名一致) - nginx: 合并 /api/ + /pms/api/ 为 /api/pms/ - 新增 docs/API.md 接口文档 - 后端: CabinetDetail 增加 rssi/pow_fail/address/soc 等字段 - Redis 管道批量读取仓体实时数据(SOC/电压/电流/功率) - DB: 所有 TIMESTAMP 列改为 DATETIME(修复类型不匹配) - 用户管理: 新增创建时间/修改时间列,状态改用 Switch - 角色管理/用户管理: 操作按钮改为图标 - PC 设备详情: 基本信息补全 RSSI/ICCID/电源/地址 - PC 卡片: 通道统计改用 Lucide 图标,颜色统一 - PC 二维码: canvas 渲染,含编号文字,支持右键下载 - H5: 按 uni-app 规范修正导航(navigateTo 替代 reLaunch) - H5: 详情页加返回导航条 - H5: 扫码支持摄像头(jsQR)+ ?id= 参数 - H5: 底部 tabBar 在所有页面常显 - H5: 首页统计改为 6 项(设备/通道/空闲/充电/充满/故障) - H5: 下拉刷新 / 上拉加载 - 产品首页: 添加业务联系 - 文档: 清理过时文件,补充缺失规范章节
6.1 KiB
6.1 KiB
title, impact, impactDescription, type, tags
| title | impact | impactDescription | type | tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| Know When to Use Directives vs Components | MEDIUM | Using directives when components are more appropriate leads to harder maintenance and testing | best-practice |
|
Know When to Use Directives vs Components
Impact: MEDIUM - Accessing the component instance from within a custom directive is often a sign that the directive should rather be a component itself. Directives are designed for low-level DOM manipulation, while components are better for encapsulating behavior that involves state, reactivity, or complex logic.
Choosing the wrong abstraction leads to code that's harder to maintain, test, and reuse.
Task Checklist
- Use directives for simple, stateless DOM manipulations
- Use components when you need encapsulated state or complex logic
- If accessing
binding.instancefrequently, consider using a component instead - If the behavior needs its own template, use a component
- Consider composables for stateful logic that doesn't need a template
Decision Matrix
| Requirement | Use Directive | Use Component | Use Composable |
|---|---|---|---|
| DOM manipulation only | Yes | - | - |
| Needs own template | - | Yes | - |
| Encapsulated state | - | Yes | Maybe |
| Reusable behavior | Yes | Yes | Yes |
| Access to parent instance | Avoid | - | Yes |
| SSR support needed | Avoid | Yes | Yes |
| Third-party lib integration | Yes | - | Maybe |
| Complex reactive logic | - | Yes | Yes |
Directive-Appropriate Use Cases
// GOOD: Simple DOM manipulation
const vFocus = {
mounted: (el) => el.focus()
}
// GOOD: Third-party library integration
const vTippy = {
mounted(el, binding) {
el._tippy = tippy(el, binding.value)
},
updated(el, binding) {
el._tippy?.setProps(binding.value)
},
unmounted(el) {
el._tippy?.destroy()
}
}
// GOOD: Event handling that Vue doesn't provide
const vClickOutside = {
mounted(el, binding) {
el._handler = (e) => {
if (!el.contains(e.target)) binding.value(e)
}
document.addEventListener('click', el._handler)
},
unmounted(el) {
document.removeEventListener('click', el._handler)
}
}
// GOOD: Intersection Observer
const vLazyLoad = {
mounted(el, binding) {
const observer = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting) {
el.src = binding.value
observer.disconnect()
}
})
observer.observe(el)
el._observer = observer
},
unmounted(el) {
el._observer?.disconnect()
}
}
Component-Appropriate Use Cases
<!-- GOOD: Component with template and state -->
<!-- Tooltip.vue -->
<template>
<div class="tooltip-wrapper" @mouseenter="show" @mouseleave="hide">
<slot></slot>
<Transition name="fade">
<div v-if="isVisible" class="tooltip-content">
{{ content }}
</div>
</Transition>
</div>
</template>
<script setup>
import { ref } from 'vue'
defineProps({
content: String
})
const isVisible = ref(false)
const show = () => isVisible.value = true
const hide = () => isVisible.value = false
</script>
<!-- GOOD: Component with complex logic -->
<!-- InfiniteScroll.vue -->
<template>
<div ref="container">
<slot></slot>
<div v-if="loading" class="loading-indicator">
<slot name="loading">Loading...</slot>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
const props = defineProps({
threshold: { type: Number, default: 100 }
})
const emit = defineEmits(['load-more'])
const container = ref(null)
const loading = ref(false)
// Complex scroll logic with state management
const handleScroll = () => {
if (loading.value) return
const { scrollHeight, scrollTop, clientHeight } = container.value
if (scrollHeight - scrollTop - clientHeight < props.threshold) {
loading.value = true
emit('load-more', () => { loading.value = false })
}
}
onMounted(() => container.value?.addEventListener('scroll', handleScroll))
onUnmounted(() => container.value?.removeEventListener('scroll', handleScroll))
</script>
Composable-Appropriate Use Cases
// GOOD: Reusable stateful logic without template
// useClickOutside.js
import { onMounted, onUnmounted, ref } from 'vue'
export function useClickOutside(elementRef, callback) {
const isClickedOutside = ref(false)
const handler = (e) => {
if (elementRef.value && !elementRef.value.contains(e.target)) {
isClickedOutside.value = true
callback?.(e)
}
}
onMounted(() => document.addEventListener('click', handler))
onUnmounted(() => document.removeEventListener('click', handler))
return { isClickedOutside }
}
// Usage in component
const dropdownRef = ref(null)
const { isClickedOutside } = useClickOutside(dropdownRef, () => {
isOpen.value = false
})
Anti-Pattern: Directive Accessing Instance Too Much
// ANTI-PATTERN: Directive relying heavily on component instance
const vBadPattern = {
mounted(el, binding) {
// Accessing instance too much = should be a component
const instance = binding.instance
instance.someMethod()
instance.someProperty = 'value'
instance.$watch('someProp', (val) => {
el.textContent = val
})
}
}
// BETTER: Use a component or composable
// Component version
<template>
<div>{{ someProp }}</div>
</template>
<script setup>
const props = defineProps(['someProp'])
</script>
When Instance Access is Acceptable
// OK: Minimal instance access for specific needs
const vPermission = {
mounted(el, binding) {
// Checking a global permission - acceptable
const userPermissions = binding.instance.$store?.state.user.permissions
if (!userPermissions?.includes(binding.value)) {
el.style.display = 'none'
}
}
}