Angular 2:可从组件访问外部js文件变量 [英] Angular 2 : external js file variables are accessible from component

查看:81
本文介绍了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文件变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆