feat(runtime): 实现类名规范化功能

- 添加 normalizeClass 函数用于处理类名数组和对象
- 在 createVNode 中集成类名规范化逻辑
- 导出 normalizeClass 供外部使用
- 创建测试 HTML 示例验证类名功能
- 处理字符串、数组和对象类型的类名输入
This commit is contained in:
dj
2026-02-27 21:00:53 +08:00
parent abf8112359
commit 026f4a5958
4 changed files with 61 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
export { ShapeFlags } from './shapeFlags'
export { normalizeClass } from './normalizeProp'
//判断是否为一个数组
export const isArray = Array.isArray

View File

@@ -0,0 +1,24 @@
import { isArray, isObject, isString } from '@vue/shared'
export function normalizeClass(value: unknown): string {
let res = ''
if (isString(value)) {
res = value
} else if (isArray(value)) {
for (let i = 0; i < value.length; i++) {
const normalized = normalizeClass(value[i])
if (normalized) {
res += normalized + ' '
}
}
} else if (isObject(value)) {
for (const name in value as object) {
if ((value as object)[name]) {
res += name + ' '
}
}
}
return res.trim()
}