vuetify、nuxt报错lh.at is not a functionlh.at‘ is undefined
报错:lh.at is not a function.(in 'h.at(-1)' 'lh.at' is undefined)
场景:
"vuetify": "~3.8.0"
"@vueuse/nuxt": "^13.0.0",
解决方案:
## 创建polyfill插件
在 plugins 目录下创建一个新的polyfill文件:
1. 文件名 : polyfill.client.ts - .client 后缀确保这个polyfill只在客户端运行
2. 功能 : 为 Array.prototype.at() 方法提供完整的polyfill实现
3. 兼容性 : 支持正数和负数索引,完全兼容原生 at() 方法的行为
4. 扩展性 : 同时为TypedArray类型添加polyfill支持
polyfill的工作原理
- 正数索引 : arr.at(0) 等同于 arr[0]
- 负数索引 : arr.at(-1) 等同于 arr[arr.length - 1]
- 越界处理 : 返回 undefined (与原生行为一致)
// Array.at() polyfill for older browsers
export default defineNuxtPlugin(() => {// 检查是否需要polyfillif (!Array.prototype.at) {Array.prototype.at = function(index: number) {// 处理正数索引if (index >= 0) {return this[index];}// 处理负数索引else {return this[this.length + index];}};console.log('Array.at() polyfill loaded for compatibility');}// 同时为TypedArray添加polyfill(如果需要)const typedArrays = [Int8Array, Uint8Array, Uint8ClampedArray,Int16Array, Uint16Array,Int32Array, Uint32Array,Float32Array, Float64Array,BigInt64Array, BigUint64Array];typedArrays.forEach(TypedArray => {if (TypedArray.prototype && !TypedArray.prototype.at) {TypedArray.prototype.at = function(index: number) {if (index >= 0) {return this[index];} else {return this[this.length + index];}};}});
});