Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angular cli项目中的外部JavaScript库引发$root.document未定义的错误_Javascript_Angular_Angular Cli - Fatal编程技术网

angular cli项目中的外部JavaScript库引发$root.document未定义的错误

angular cli项目中的外部JavaScript库引发$root.document未定义的错误,javascript,angular,angular-cli,Javascript,Angular,Angular Cli,我试图学习如何在angular cli生成的项目中使用外部JavaScript库。我正在编写一个在浏览器中呈现分子的应用程序,所以我使用 这个库在npm上不可用,所以我必须手动导入它,以便webpack知道它存在。我按照以下步骤在main.ts中声明变量: import './polyfills.ts'; window['$'] = require('jquery'); window['Kekule'] = require("../libs/kekule/kekule.js"); impor

我试图学习如何在angular cli生成的项目中使用外部JavaScript库。我正在编写一个在浏览器中呈现分子的应用程序,所以我使用

这个库在npm上不可用,所以我必须手动导入它,以便webpack知道它存在。我按照以下步骤在main.ts中声明变量:

import './polyfills.ts';

window['$'] = require('jquery');
window['Kekule'] = require("../libs/kekule/kekule.js");

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);
这似乎初始化了Kekule.js并将
$root
变量传递给它

然而,我在Kekule.js中运行
init()
方法时遇到了一个错误。它调用另一个方法,该方法使用脚本标记查找DOM中的所有元素:
$root.document.getElementsByTagName('script')
,但引发以下错误:

Uncaught TypeError: Cannot read property 'getElementsByTagName' of undefined
    at analysisEntranceScriptSrc (kekule.js:462)
    at init (kekule.js:574)
    at kekule.js:614
    at Object.<anonymous> (kekule.js:616)
    at Object.682 (main.bundle.js:910)
    at __webpack_require__ (bootstrap dcc310b…:52)
    at Object.469 (index.ts:2)
    at __webpack_require__ (bootstrap dcc310b…:52)
    at Object.467 (main.ts:15)
    at __webpack_require__ (bootstrap dcc310b…:52)
Uncaught TypeError:无法读取未定义的属性“getElementsByTagName”
位于SRC(kekule.js:462)
在init(kekule.js:574)
在kekule.js:614
反对。(kekule.js:616)
at Object.682(main.bundle.js:910)
at\uuuuu网页包\uuuuuuuuuuuuuu需要(引导dcc310b…:52)
at Object.469(索引ts:2)
at\uuuuu网页包\uuuuuuuuuuuuuu需要(引导dcc310b…:52)
at Object.467(main.ts:15)
at\uuuuu网页包\uuuuuuuuuuuuuu需要(引导dcc310b…:52)
我做了一些测试,当脚本试图运行时,
$root.document
确实是未定义的。我曾尝试在index.html中加载脚本标记,但随后抛出了typescript错误,即对Kekule的变量引用没有在我的typescript类中定义

  • 发生这种情况是因为angular提供了错误的
    $root
    执行上下文
  • 还是因为angular在DOM之前加载了脚本 创造了什么
  • 还有别的问题吗
  • 如果您能帮助理解/解决此问题,我们将不胜感激