-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?

0

1 Answer 1

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"
      }
    }
  ],

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

3 Comments

@Komal_Chandra, update me whether it worked in your case
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".
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.