【问题标题】:zone.js:25 Uncaught Error: Zone already loadedzone.js:25 未捕获的错误:区域已加载
【发布时间】:2016-10-26 00:05:37
【问题描述】:

我的 Angular 应用程序在单击按钮时加载。当第二次点击按钮时,它给我Zone already loaded 错误,因为 zone.js 内容是在第一次加载 angular2 时加载的。

我将如何检查该区域是否已加载,然后可以阻止尝试加载它(如果可用)?

当我删除delete window['zone'] 的区域(在下面回答)并尝试重新加载它时,它显示zone.js:984 Uncaught ReferenceError: Zone is not defined

【问题讨论】:

  • 请添加一些代码来演示您的问题
  • 我正在使用 angular-cli 来构建我的项目。一旦它构建它只创建 2 个文件,包括 zone.js 并且我在单击按钮时加载我的 Angular 应用程序,当第二次单击按钮时它会显示错误。
  • 重点是我可以通过 angular-cli 编辑创建的文件并检查区域是否已加载,但我怎样才能从 angular-cli 或通过其他方式自动执行此操作。
  • @yurzui, code: var Zone$1 = (function (global) { if (global.Zone) { throw new Error('Zone already loaded.'); } 来自angular-cli构建生成的代码
  • 第二次尝试包含 zone.js 脚本时会出现此错误。

标签: angular zone


【解决方案1】:

你可以试试这样的……

$(function (){
		function removejscssfile(filename, filetype){
		   var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
		   var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
		   var allsuspects=document.getElementsByTagName(targetelement)
		   for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
		   if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
		       allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
		   }
		}
	var jsCustomerTag = $("head").children("#jsShimTag");
    if (jsCustomerTag.length == 0) {
    	jsCustomerTag = document.createElement('script');
    	jsCustomerTag.setAttribute("type", "text/javascript");
    	jsCustomerTag.setAttribute("src", " <your js file> ");
    	jsCustomerTag.id = "jsShimTag";
        document.getElementsByTagName("head")[0].appendChild(jsCustomerTag);
    }

   jsCustomerTag = null;
});

【讨论】:

  • 这是从浏览器中删除脚本并重新加载的一种方法。让我试试
猜你喜欢
  • 2017-07-13
  • 2018-04-19
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 2018-10-16
  • 1970-01-01
  • 2020-02-07
相关资源
最近更新 更多