谁能告诉我为什么
(<any>window).dataLayer2 = (<any>window).dataLayer2 || [];
不抛出错误并且
(<any>window).dataLayer = (<any>window).dataLayer || [];
是否抛出类型错误:“错误:无法解析‘dataLayer’”
我需要能够访问 google analytics 全局变量,但不确定为什么这不起作用或如何为 ES6 公开它。
我正在使用 webpack 和 typescript 并将其添加到我的 index.ejs 中,如下所示:
<body aurelia-app="main">
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-okoko" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-ookok');
</script>
最佳答案
我正在使用 TypeScript 2.9.2
打字.d.ts
interface Window { dataLayer: any; }
应用.ts
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({...});
有关更多信息,请参阅这些其他答案:
How do you explicitly set a new property on `window` in TypeScript?
How to handle warnings for proprietary/custom properties of built-in objects in TypeScript
关于javascript - Google 跟踪代码管理器数据层类型错误 : ES6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45325631/