---
name: frontend-code-review
在以下场景激活此技能:
- 审查 Pull Request
- 进行 Vue 组件代码审查
- 检查安全漏洞
- 验证 TypeScript 类型安全
- 审查性能优化
此技能提供前端代码审查的检查清单和指南。
any 类型import type 语法<script setup> 语法computed vs watchonUnmounted 中清理副作用v-dompurify-html)v-memo 和 v-once// 错误
props.value = newValue;
// 正确
emit('update:value', newValue);
// 错误 - 没有清理
onMounted(() => {
window.addEventListener('resize', handleResize);
});
// 正确 - 组件卸载时清理
onMounted(() => {
window.addEventListener('resize', handleResize);
});
onUnmounted(() => {
window.removeEventListener('resize', handleResize);
});
// 错误 - 静态数据不需要响应式
const menuItems = reactive([
{ label: '首页', path: '/' },
{ label: '关于', path: '/about' },
]);
// 正确 - 静态数据不需要响应式
const menuItems = [
{ label: '首页', path: '/' },
{ label: '关于', path: '/about' },
];
<!-- 错误 - 没有错误处理 -->
<template>
<ExpensiveComponent />
</template>
<!-- 正确 - 有错误边界 -->
<template>
<ErrorBoundary>
<ExpensiveComponent />
</ErrorBoundary>
</template>
v-html 但没有过滤window.location 进行 URL 注入eval() 或 Function()使用建设性的评论:
**建议**: 这里考虑使用 `computed` 替代 `watch` 以获得更好的性能。
// 当前代码
watch(() => props.value, (val) => {
doubled.value = val * 2;
});
// 建议修改
const doubled = computed(() => props.value * 2);
```
原因: 计算属性有缓存,只有依赖变化时才重新计算。
```