我是 Angular 的新手。我在 scripts 标签内的 angular-cli.json 中添加了 JS 文件。文件已成功加载,但问题是在我的 LoginForm 控制台上显示:
zone.js:192 Uncaught ReferenceError: theWYSIWYG is not defined
它在我成功登录时加载 我从 login-form.component.ts
this.router.navigate(['dashboard']);
路由到我的另一个组件> 在仪表板组件上,一些 html 代码是
<div id="theWYSIWYG" name="theWYSIWYG" frameborder="0" contentEditable="true" dir="rtl" ></div>
在应用程序启动时加载的 js 文件中定义的函数。为什么 JS 不访问仪表板组件。?
最佳答案
如果你想在你的组件中使用javascript。你必须包含在@Component 之前
declare var require: any;
const myFunc = require('../assets/jsFile');
@Component({ ....
然后在您的 AppComponent 中,您可以使用 myFunc 调用 javascript 函数。
ngOnInit() {
myFunc.func1();
}
关于javascript - Angular4加载JS文件的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49954586/