未捕获的SyntaxError:在严格模式下出现意外的eval或参数:window.gtag =(参数)=> dataLayer.push(arguments); [英] Uncaught SyntaxError: Unexpected eval or arguments in strict mode: window.gtag = (arguments) => dataLayer.push(arguments);
问题描述
我有以下代码:
class MetricGoogleGateway extends AMetricGateway{
constructor(id, name, token) {
super(id, name);
this.token = token;
}
configure() {
if(!window.dataLayer && !window.gtag)
{
window.dataLayer = window.dataLayer || [];
window.gtag = (arguments) => dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'UA-113071675-1');
}
}
通过浏览器加载文件时,出现以下错误:
When the file is loaded by the browser, I got the following error:
未捕获到的SyntaxError:严格模式下出现意外的eval或参数
Uncaught SyntaxError: Unexpected eval or arguments in strict mode
但是,如果我在chrome控制台中运行以下行,则一切正常:
But if I I run the following lines in the chrome console, everything its ok:
window.dataLayer = window.dataLayer || [];
window.gtag = (arguments) => dataLayer.push(arguments);
window.gtag(1);
window.gtag(2);
console.log(window.dataLayer)
-> console.log result: [1,2]
注意:
我注意到,如果我改变了这一行:
I notice that if I changed the line:
window.gtag = (arguments) => dataLayer.push(arguments);
对于该行:
window.gtag = function(arguments) { dataLayer.push(arguments) }
我遇到相同的错误
推荐答案
Your code is inside a class
body which runs in strict mode (as all your code should), unlike the snippet pasted into the console.
名称arguments
和eval
是特殊的,禁止将其用作标识符.不要使用它们.只需写args
,或者甚至写出更好的东西,例如layer
.
The names arguments
and eval
are special and forbidden as identifiers. Don't use them. Just write args
instead, or even better something actually meanigful like layer
.
window.gtag = (layer) => dataLayer.push(layer);
这篇关于未捕获的SyntaxError:在严格模式下出现意外的eval或参数:window.gtag =(参数)=> dataLayer.push(arguments);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!