繁体   English   中英

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

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

我需要在我的 angular 2 项目中使用这个 JS 库

这个问题可能对我来说是重复的,但没有答案对我有用

我试图将库作为脚本标记包含在我的 index.html 页面中

它总是看不到它http://localhost:8100/PrayTimes.js文件不存在

我也在上面写了这段代码

declare var PrayTimes:any;

我试图在我的构造函数中使用它,但出现此错误

PrayTimes 未定义

如果使用angular-cli,则可以在assets文件夹中添加所有外部JS文件。 然后在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"
      ]

您也可以使用外部样式执行此操作:

"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"
      ]

当然你是对的,然后你需要添加typings.d.ts

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

将所有javascript,外部CSS,图像等放在src/assets

(将编译为build/assets

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

然后你可以像你描述的那样使用它。 declare var PrayTimes: any;

我需要在外部库中使用 function,所以我没有声明 JS class,而是声明了 JS function。

declare function zoom(t: any);

暂无
暂无

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

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