23

I started a project with Angular but I never thought that install the most recent version of it would bring me a couple of problems. I am also using Materialize so when I try to 'import' its Javascript file it doesn't work. I don't know why, I was looking for an answer since the past Friday but I found nothing.

I have changed the angular.json file and referenced my JS location in it but it doesn't was sufficient.

P.D. I must not use the CDN for materialize JS.

12
  • You can import in package script or in index.html Commented Jun 13, 2018 at 16:32
  • Are you using CLI? Commented Jun 13, 2018 at 16:34
  • Yeah, I am using CLI. I tried to import it in the index but it did not work Commented Jun 13, 2018 at 16:36
  • you can add "scripts": [ "../node_modules/jquery/dist/jquery.js" ], in angular-cli.json Commented Jun 13, 2018 at 16:39
  • You can also have another way of adding materialize in you project. Check the link: npmjs.com/package/angular2-materialize Commented Jun 13, 2018 at 16:41

3 Answers 3

19

CLI projects in angular 6 onwards uses angular.json instead of .angular-cli.json for build and project configuration. That implies you are using Angular 6.
As of v6, the location of the file has changed to angular.json. Since there is no longer a leading dot, the file is no longer hidden by default and is on the same level. which also means that file paths in angular.json should not contain leading dots and slash i.e you should provide an absolute path

Install MaterializeCSS and angular2-materialize from npm

 npm install materialize-css --save 
 npm install angular2-materialize --save 
 npm install jquery@^2.2.4 --save
 npm install hammerjs --save

After installing all the required dependencies add them to styles and scripts array of angular.json

"styles": [
             
      "src/styles.css",
      "node_modules/materialize-css/dist/css/materialize.css"
],
"scripts": [
      "node_modules/jquery/dist/jquery.js",
       "node_modules/hammerjs/hammer.js",
       "node_modules/materialize-css/dist/js/materialize.js"
 ]

For Angular Version 11+

Configuration

The styles and scripts options in your angular.json configuration now allow to reference a package directly:

before: "styles": ["../node_modules/bootstrap/dist/css/bootstrap.css"]
after: "styles": ["bootstrap/dist/css/bootstrap.css"]

P.S
Additonal Info: Error import javascript library in typescript

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

10 Comments

Yup, actually I have this: "scripts": [ "node_modules/materialize-css/dist/js/materialize.js" ]
Have you maintained the same order while adding it?
I do not know why the materialize.css works and the materialize.js does not. I used the same reference (obviously changing the folder css to js and the files extension)
try npm install --save @types/jquery
how can I add custom js file?
|
3

I think that key is: correct place on configuration. We have two places when we can potentially paste the paths for scripts. (Angular CLI: 6.1.5)

The first one is: (projects => your app name => architect => build)

"build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "outputPath": "dist/<your app name>",
        "index": "src/index.html",
        "main": "src/main.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "src/tsconfig.app.json",
        "assets": [
          "src/favicon.ico",
          "src/assets"
        ],
        "styles": [
          "src/styles.css"
        ],
        "scripts": [
          "node_modules/jquery/dist/jquery.min.js",
          "node_modules/bootstrap/dist/js/bootstrap.min.js"
        ]
      },
      "configurations": { ... }
    }

The second one is: (projects => your app name => architect => test) - WRONG PLACE

"test": {
      "builder": "@angular-devkit/build-angular:karma",
      "options": {
        "main": "src/test.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "src/tsconfig.spec.json",
        "karmaConfig": "src/karma.conf.js",
        "styles": [
          "src/styles.css"
        ],
        "scripts": [
        ],
        "assets": [
          "src/favicon.ico",
          "src/assets"
        ]
      }
    }

After that you shoud have something like this on your browser:

enter image description here

I hope that it helped ;)

Comments

0

js always write in script tag with "".

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "Vhls": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/Vhls",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/videogular2/fonts/videogular.css",
              "src/styles.scss"
            ],
            "scripts": [
              "src/assets/js/hls.min.js"
            ]
          },
          "configurations": {...............

Comments

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.