Pawan
Pawan

Reputation: 31

Use external JavaScript file's in angular 7

I'm trying to use external JavaScript files in angular 7 project and for this I have added multiple JavaScript files to angular.json file as in image

JavaScript files added in angular.json file

and also loading default multiple JavaScript files on page with make any reference manual so, from where they are refereed.

Please suggest the right way to use external JavaScript files in Angular 7 and also suggest how should we use the packages JavaScript files in Angular 7.

Thanks in advance.

Upvotes: 0

Views: 4313

Answers (1)

mwilson
mwilson

Reputation: 12980

To import jQuery into an Angular app, you need to install it properly. Opinionated answer below.

  1. npm install jquery --save
  2. import $ from 'jquery';
  3. Use jQuery
  4. Think long and hard about why you are using jQuery still

https://stackblitz.com/edit/angular-8kcpz2?file=src%2Fapp%2Fhello.component.ts

import { Component, Input } from '@angular/core';
import $ from 'jquery';

@Component({
  selector: 'hello',
  template: `<h1>Why are you still using jquery? </h1>`,
  styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent implements OnInit  {
  @Input() name: string;
  consturctor() {}
  ngOnInit() {
    alert($('h1').text())
  }
}

You do not need to add it to your angular.json file.

Upvotes: 1

Related Questions