开发完vue项目,本地运行正常,却在很少的一部分低版本的手机里出现了repeat is not defined的报错(抓包)
然后查了原因 最终在MDN找到了解决方法
主要原因是
此方法已添加到ECMAScript 2015规范中,并且可能尚未在所有JavaScript实现中可用,所以这里我们需要做一下扩展
if (!String.prototype.repeat) {
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
var str = '' + this;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count == Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return '';
}
// 确保 count 是一个 31 位的整数。这样我们就可以使用如下优化的算法。
// 当前(2014年8月),绝大多数浏览器都不能支持 1 << 28 长的字符串,所以:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var rpt = '';
for (;;) {
if ((count & 1) == 1) {
rpt += str;
}
count >>>= 1;
if (count == 0) {
break;
}
str += str;
}
return rpt;
}
}
如果是vue项目出现这种错 可以直接新建一个文件,然后全局引用,这样就能成功的解决这个问题
附上链接 兼容处理