简体   繁体   English

在我的 angular 2 项目中使用外部 JS 库

[英]using external JS libraries in my angular 2 project

I need to use this JS library in my angular 2 project我需要在我的 angular 2 项目中使用这个 JS 库

this question may be duplicate with me, but no answer worked for me这个问题可能对我来说是重复的,但没有答案对我有用

I tried to include the library as script tag in my index.html page我试图将库作为脚本标记包含在我的 index.html 页面中

It always does not see it http://localhost:8100/PrayTimes.js file is not exist它总是看不到它http://localhost:8100/PrayTimes.js文件不存在

also I wrote this code above我也在上面写了这段代码

declare var PrayTimes:any;

I tried to use it in my constructor, but I am getting this error我试图在我的构造函数中使用它,但出现此错误

PrayTimes is not defined PrayTimes 未定义

If you use angular-cli, you can add all your external JS files in assets folder. 如果使用angular-cli,则可以在assets文件夹中添加所有外部JS文件。 And then in angular-cli.json add them: 然后在angular-cli.json添加它们:

"scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/bootstrap/dist/js/bootstrap.min.js",
        "../node_modules/moment/moment.js",
        "../node_modules/chart.js/dist/Chart.bundle.min.js",
        "../node_modules/chart.js/dist/Chart.min.js",
        "../node_modules/ng2-datetime/src/vendor/bootstrap-datepicker/bootstrap-datepicker.min.js",
        "./assets/js/slimscroll.min.js",
        "./assets/js/inspinia.js",
        "./assets/js/metisMenu.js",
        "./assets/js/footable.all.min.js"
      ]

You can do it also with external styles: 您也可以使用外部样式执行此操作:

"styles": [
        "../node_modules/ng2-toastr/bundles/ng2-toastr.min.css",
        "../node_modules/bootstrap-sass/assets/stylesheets/_bootstrap.scss",
        "../node_modules/font-awesome/scss/font-awesome.scss",
        "../node_modules/ng2-datetime/src/vendor/bootstrap-datepicker/bootstrap-datepicker3.min.css",
        "./assets/scss/plugins/footable/footable.core.css",
        "./assets/scss/style.scss"
      ]

And of course you are right, then you need to add in typings.d.ts : 当然你是对的,然后你需要添加typings.d.ts

declare var PrayTimes:any;
declare var System: any;
declare var $: any;
declare var moment: any;
declare var Chart: any;

Place all javascript, external css, images etc. in src/assets 将所有javascript,外部CSS,图像等放在src/assets

(will be compiled to build/assets ) (将编译为build/assets

In your index.html : <script src="assets/yourJavascript.js"></script> index.html<script src="assets/yourJavascript.js"></script>

Then you can just use it like you describe. 然后你可以像你描述的那样使用它。 ( declare var PrayTimes: any; ) declare var PrayTimes: any;

I needed to use a function inside of an external library, so rather than declaring the JS class, I declared the JS function.我需要在外部库中使用 function,所以我没有声明 JS class,而是声明了 JS function。

declare function zoom(t: any);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM