简体   繁体   English

在Angular 8中添加外部库

[英]Add external libraries in Angular 8

I want to use some external jQuery libraries in my Angular project. 我想在Angular项目中使用一些外部jQuery库。

I added these libraries inside index.html file, but they are not working. 我在index.html文件中添加了这些库,但是它们不起作用。 I also added my .js files in angular.json , but the problem still exists. 我还在我的.js文件中添加了angular.json文件,但问题仍然存在。 I add libraries like this: 我添加如下库:

 <!-- CUSTOM JS --> <script src="assets/js/jquery.app.js"></script> 

Actually the code in this js file is not reachable. 实际上,此js文件中的代码无法访问。 It seems Angular does not understand it to go and run that specific function in this file. 似乎Angular不了解它是否可以在该文件中运行该特定功能。

I expect a menu item shows its sub menus but the jQuery code does not work in Angular. 我希望菜单项显示其子菜单,但jQuery代码在Angular中不起作用。

As you use Angular, you probably also use the angular/cli. 使用Angular时,可能还使用了angular / cli。 In that case, just add your file in the angular.json file like : 在这种情况下,只需将您的文件添加到angular.json文件中即可:

  "scripts": [
    "../node_modules/jquery/dist/jquery.js"
  ],

This will let angular import your external lib in the end bundle. 这将使您将外部lib角导入最终包中。

You will find more informations in the official documentation : https://github.com/angular/angular-cli/wiki/stories-global-scripts 您将在官方文档中找到更多信息: https : //github.com/angular/angular-cli/wiki/stories-global-scripts

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

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