简体   繁体   English

如何在Angular版本7中将外部javascript库文件添加到特定组件

[英]How to add a external javascript library file to a specific component in Angular version 7

I'm a newbie with Angular framework. 我是Angular框架的新手。 While I trying to create a HTML template using Angular version 7. I fall in to a problem about javascript library. 当我尝试使用Angular版本7创建HTML模板时,我遇到了有关javascript库的问题。 My template has a lot a page such as home, about, product, etc... At a home page, I need to display a slide using jquery.slidey.js library. 我的模板上有很多页面,例如主页,关于,产品等。在主页上,我需要使用jquery.slidey.js库显示一张幻灯片。 I'm using script tag to add the library into home.component.html but the library is not loaded. 我正在使用脚本标签将库添加到home.component.html中,但未加载该库。 So the question is How can add a external javascript into a specific Angular component. 所以问题是如何将外部javascript添加到特定的Angular组件中。 Thanks in advance 提前致谢

You can see all my source at My GitHub source code 您可以在My GitHub源代码中查看我的所有源代码

See this . 看到这个 The link shows how to install, setup, and import JQuery library into an Angular project with details and images you can easily follow. 该链接显示了如何将JQuery库安装,设置和导入到Angular项目中,其中包含您可以轻松遵循的详细信息和图像。 There is also a working demo in at the bottom. 底部还有一个工作示例。

This is how I resolved it. 这就是我解决的方法。

Index.html 的index.html

<head>
 ...
 <link rel="icon" type="image/x-icon" href="favicon.ico">
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

HomeComponent.ts HomeComponent.ts

 import { Component, OnInit } from '@angular/core'
 declare var $: any;


export class HomeComponent implements OnInit {

  constructor(private appService: AppService) { }

  ngOnInit() {
  }

  doSomething() {
    $("p").hide(); //example
  }

}

First, install this package npm install jquery-slider 首先,安装此软件包npm install jquery-slider

Now, open angular.json file & add below styles & scripts. 现在,打开angular.json文件并添加以下样式和脚本。

"styles": [ "./node_modules/jquery-slider/..CSS File Path", ], "scripts": [ "./node_modules/jquery-slider/...JS File Path" ]

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

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