0

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.

8
  • 2
    What's wrong with what you've done? Commented Dec 3, 2018 at 18:20
  • 5
    Please don't use a link to an image. Some people cannot have access. Add the code directly to your question. Thanks. Commented Dec 3, 2018 at 18:22
  • Are you using npm or is this just some file laying around? Using import ... should do the trick so long as it's a proper module Commented Dec 3, 2018 at 18:22
  • Why are you importing jQuery into Angular? Gross. Commented Dec 3, 2018 at 18:23
  • 1
    Where is mentioned these files in Angular 7 - please do some research on @angular/cli, what happens when you do ng serve or ng build, etc. Commented Dec 3, 2018 at 18:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for sharing this answer @mwilson but I want to know that as I mentioned previously that on browser there are some JavaScript files were loaded without any referenced, Where is this configured these files and as your answered, import { Component, Input } from '@angular/core'; Jquery is loaded using this but where is mentioned these files. Sorry If I'm asking something working I'm new in Angular
That's a hard question to answer if not impossible. Angulars Webpack bundling process will pull in anything being referenced. So, let' say you want to use bootstrap (like you're doing) and you go to install bootstrap with npm install bootstrap. Well. bootstrap requires jquery and a couple other modules so those will be installed too. You may not have explicitly installed, jQuery, but you're using a package that requires it.
There are some tools out there that might help you sniff out what is requiring jQuery, but from the screen shot, bootstrap is probably the culprit. The only places you can load scripts in an angular app are either through a direct script tag in the index.html, adding scirpts to the angular.json file, importing through npm and any other direct references in your webpack process.
Now I simply adding only three files in angular.json file which are not have any dependency of other files ` "styles": [ "src/styles.scss" ], "scripts": [ "src/assets/scripts/fastclick/fastclick.min.js", "src/assets/scripts/adminlte/adminlte.min.js", "src/assets/scripts/adminlte/demo.js" ] ` but still they are not showing on browser in script tag (Not showing in browser).
That's because you're not importing them correctly. You need to learn more about how angular works. Angular is not something where you can just throw a script in and expect it to work. You need to use es6 import statements and use npm to work with 3rd part libraries. If you have a custom script you're trying to use and it's way out of date, just throw it in the index.html as a script tag and declare a global variable for it so TypeScript can properly ignore it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.