本文介绍了Angular 2:可以从组件访问外部 js 文件变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将外部 js 文件导入到我的组件中.
I am trying to import an external js file to my component.
external.js
external.js
var x=9;
component.ts
component.ts
import '../../assets/api/external.js';
declare let x: any;
....
console.log(x);
我得到:
ERROR ReferenceError: x is not defined
如何从组件访问 x?它不会抱怨找不到js文件.
How can I access x from the component? It does not complain about not being able to find the js file.
推荐答案
请将您的脚本添加到 angular-cli.json 中作为全局脚本.
please add your script in angular-cli.json as a global script.
"scripts": [
"../external.js",
"../node_modules/.....min.js",
"../node_modules/jspdf-autotable/dist/....js"
],
如果有帮助,请告知.这是参考链接:https://github.com/angular/angular-cli/wiki/stories-global-scripts
let know if it helps.Here is the reference link: https://github.com/angular/angular-cli/wiki/stories-global-scripts
这篇关于Angular 2:可以从组件访问外部 js 文件变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!