简体   繁体   English

窗口变量声明在React的index.js中不起作用

[英]Window variable declaration not working in React's index.js

I am trying to set up Heap Analytics in my React app. 我正在尝试在我的React应用程序中设置Heap Analytics。 I have an index.js ( no custom index.html). 我有一个index.js(没有自定义index.html)。

In the instructions, it's saying that I should put the script in between tag in the head. 在说明中,这是说我应该将脚本放在标签之间。 I assume that for my case, I just do it in index.js. 我认为就我而言,我只是在index.js中做到了。

This is the script: 这是脚本:

window.heap=window.heap||[],heap.load=function(e,t)
{window.heap.appid=e,window.heap.config=t=t||{};var r=t.forceSSL||"https:"===document.location.protocol,a=document.createElement("script");a.type="text/javascript",a.async=!0,a.src=(r?"https:":"http:")+"//cdn.heapanalytics.com/js/heap-"+e+".js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(a,n);for(var o=function(e){return function(){heap.push([e].concat(Array.prototype.slice.call(arguments,0)))}},p=["addEventProperties","addUserProperties","clearEventProperties","identify","removeEventProperty","setEventProperties","track","unsetEventProperty"],c=0;c<p.length;c++)heap[p[c]]=o(p[c])};
heap.load(MY_APP_ID);

However, heap is not defined error comes up. 但是, heap is not defined error出现。

What should I be doing instead? 我应该怎么做呢?

I got it to work by updating the variable declarations. 我通过更新变量声明使其工作。

var heap = [];
heap.load = ... // same as above, but all instances of window.heap are replaced with just heap
window.heap = heap;
heap.load(APP_ID)

so I think the window variable declaration doesn't play well with React. 所以我认为window变量声明不能与React很好地配合。 If anyone can explain to me exactly why and how my solution works but original script doesn't, please help enlighten me. 如果有人可以确切地向我解释我的解决方案的原因和方式,但原始脚本却无法解决,请帮助我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM