如何将JavaScript文件导入angular2 [英] How to import JavaScript file into angular2

查看:91
本文介绍了如何将JavaScript文件导入angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何将javascript文件导入我的angular2项目时遇到了麻烦.这个文件不是npm之外的模块,我唯一能找到的指令就是使用npm,这不是这里的选择.

I am having trouble figuring out how to import a javascript file into my angular2 project. It is a file that is not a module that is apart of npm and the only instructions I have been able to find is using npm which is not an option here.

我正在使用angular cli,在我的angular-cli.json文件中,我拥有:

I am using angular cli and in my angular-cli.json file I have:

{
"apps": [
  "styles": [..],
  "scripts": ["../pathtomyjs/file.js"] 
]}

我运行了build和ng服务,当我打开应用程序并查看Web控制台并尝试引用js文件中的函数时,我能够成功完成此操作.

I ran ng build and ng serve and when I open up my app and look at the web console and try referencing the function that is in my js file, I am able to do so successfully.

我在控制台中输入的示例:

Example in the console I type in:

var test = MyObject; 

然后我得到test = {};

and I get test = {};

但是,当我尝试这样做

let test = MyObject;

在我的.ts组件文件中,我得到了:

in my component .ts file I get :

home.component.ts (10,11): Cannot find name 'MyObject'.)

不确定如何在Angular2中执行此操作.

Not sure how to do this in Angular2.

谢谢!

推荐答案

执行:

declare var MyObject: any;

let test = MyObject;

或者:

let test = (<any> MyObject);

对于jQuery:

declare var jQuery: any;

jQuery.ajax({ ... });

这篇关于如何将JavaScript文件导入angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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