-2

I have a JS/jQuery function's file & want to use it in all components in my Angular 5 project. So, how can I use it's functions in my project.

Note: I've already installed jQuery and can use it as well in my typescript. But I want to use functions from my own JS/jQuery file (app.js) in typescript. How can I do that?

Bakchod Guru
  • 51
  • 1
  • 6

1 Answers1

1

you can include them in the file assets (or somewhere else) and further add them in .angular-cli.json, the path should be correct one. You can add a folder called js inside assets and place such files there. And modify the file .angular-cli.json like

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "angular-test"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        ".htaccess",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "../node_modules/font-awesome/css/font-awesome.min.css",
        "assets/css/styles.min.css",
        "styles.css"
      ],
      "scripts": [
        "assets/js/app.js"   /* add all such files HERE */
      ],
      "environmentSource": "environments/env.ts",
      "environments": {
        "dev": "environments/env.ts"
      }
    }
  ],

  ...
}
Anoop Surendran
  • 302
  • 5
  • 19
  • @Komal_Chandra, update me whether it worked in your case – Anoop Surendran Feb 28 '18 at 07:04
  • no it's not working. How can I import this JS file (app.js) in my typescript? Like to import jQuery plugin, I used "import * as $ from 'jquery". – Bakchod Guru Feb 28 '18 at 09:46
  • Usually jquery is not much used in Angular projects. However its required for some effects and all. Such simple tasks can be done by adding such files in assets, and mention in angular-cli.json. There is no need to install jquery package at all – Anoop Surendran Feb 28 '18 at 10:11