ngx-slick-carousel icon indicating copy to clipboard operation
ngx-slick-carousel copied to clipboard

ERROR TypeError: _this.$instance.slick is not a function

Open petinho opened this issue 6 years ago • 15 comments

Hi. I've included the library with the same steps as in the example, but I'm receiving the error on the title. It seems to be, that the function is not found. Imports are done correctly, and the order should also be ok.

Do you have an idea of what could be wrong?

image

$instance is not undefined, but has no slick function.

Thanks in advance

petinho avatar Sep 03 '18 15:09 petinho

@petinho It is not ngx-slick-carousel problem. Did you add slick library in angular.json?

Include slick css in "styles" at your angular.json file :

  "styles": [
    ...
    "node_modules/slick-carousel/slick/slick.scss",
    "node_modules/slick-carousel/slick/slick-theme.scss",
    ...
  ]

Include jquery and slick js in "scripts" at your angular.json file :

  "scripts": [
    ...
    "node_modules/jquery/dist/jquery.min.js",
    "node_modules/slick-carousel/slick/slick.min.js",
    ...
  ]

leo6104 avatar Sep 15 '18 08:09 leo6104

I get the same error, some solution?

dianaVidalC avatar Sep 20 '18 17:09 dianaVidalC

@petinho @dianaVidalC It is common bug when trying to use jQuery library in Angular. There is lots of causes make it happened. Please give more information about your project.

  1. What is your angular version? Please comment your ng version command result
  2. Can you share your angular.json "scripts" part?
  3. Is there any other jQuery library you use ?

leo6104 avatar Sep 20 '18 18:09 leo6104

@petinho I use boostrap too, I hope you can help me

angular version:

Angular CLI: 6.1.5
Node: 8.11.4
OS: win32 x64
Angular: 6.1.7
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.7.5
@angular-devkit/build-angular     0.7.5
@angular-devkit/build-optimizer   0.7.5
@angular-devkit/build-webpack     0.7.5
@angular-devkit/core              0.7.5
@angular-devkit/schematics        0.7.5
@angular/cli                      6.1.5
@ngtools/webpack                  6.1.5
@schematics/angular               0.7.5
@schematics/update                0.7.5
rxjs                              6.3.2
typescript                        2.7.2
webpack                           4.9.2

"scripts" part in angular.json

  "scripts": [
      "./node_modules/jquery/dist/jquery.min.js",
      "./node_modules/popper.js/dist/umd/popper.min.js",
      "./node_modules/bootstrap/dist/js/bootstrap.min.js",
      "node_modules/slick-carousel/slick/slick.min.js"
     ]

dianaVidalC avatar Sep 20 '18 18:09 dianaVidalC

@leo6104 The library and all needed scripts/styles were added.

Here is my Angular version:

` Angular CLI: 6.1.5 Node: 8.11.4 OS: win32 x64 Angular: 6.1.4 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.7.5 @angular-devkit/build-angular 0.7.5 @angular-devkit/build-optimizer 0.7.5 @angular-devkit/build-webpack 0.7.5 @angular-devkit/core 0.7.5 @angular-devkit/schematics 0.7.5 @angular/cli 6.1.5 @ngtools/webpack 6.1.5 @schematics/angular 0.7.5 @schematics/update 0.7.5 rxjs 6.2.2 typescript 2.9.2 webpack 4.9.2`

My scripts part in angular.json (slick library currently removed, but it was there):

`

"scripts": [

          "node_modules/jquery/dist/jquery.min.js",

          "node_modules/bootstrap/dist/js/bootstrap.min.js",

          "node_modules/startbootstrap-sb-admin/js/sb-admin.js",

          "node_modules/jquery.easing/jquery.easing.min.js",

          "node_modules/chart.js/src/chart.js"

        ]

`

petinho avatar Sep 21 '18 12:09 petinho

any idea?

Angular CLI: 7.1.3 Node: 10.11.0 OS: darwin x64 Angular: 7.1.3 ... animations, cli, common, compiler, compiler-cli, core, forms ... language-service, platform-browser, platform-browser-dynamic ... router

Package Version

@angular-devkit/architect 0.11.3 @angular-devkit/build-angular 0.11.4 @angular-devkit/build-optimizer 0.11.4 @angular-devkit/build-webpack 0.11.4 @angular-devkit/core 7.1.3 @angular-devkit/schematics 7.1.3 @angular/cdk 7.2.1 @ngtools/webpack 7.1.4 @schematics/angular 7.1.3 @schematics/update 0.11.3 rxjs 6.3.3 typescript 3.1.6 webpack 4.23.1

asterism612 avatar Jan 15 '19 05:01 asterism612

Also having this error, anyone found a solution!?

vinims avatar Mar 11 '19 20:03 vinims

Any updates on this?

MalshaDeZ avatar Apr 04 '19 11:04 MalshaDeZ

I'm not sure whether this is the correct way, but this did the trick for me. I think the issue occurs because slick.min.js is not loaded , so loading the js file manually before it's needed fixes this issue. Hope this helps.

 constructor( private elementRef: ElementRef){}

 ngAfterViewInit() {
    this.iniSlickJs();
  }

 private iniSlickJs() {
    const htmlScriptElement = document.createElement('script');
    htmlScriptElement.src = 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.min.js';
    this.elementRef.nativeElement.appendChild(htmlScriptElement);
  }

MalshaDeZ avatar Apr 08 '19 07:04 MalshaDeZ

I'm not sure whether this is the correct way, but this did the trick for me. I think the issue occurs because slick.min.js is not loaded , so loading the js file manually before it's needed fixes this issue. Hope this helps.

 constructor( private elementRef: ElementRef){}

 ngAfterViewInit() {
    this.iniSlickJs();
  }

 private iniSlickJs() {
    const htmlScriptElement = document.createElement('script');
    htmlScriptElement.src = 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.min.js';
    this.elementRef.nativeElement.appendChild(htmlScriptElement);
  }

hi @MalshaDeZ , I found your solution prominent but how can we sure that slick.min.js is not loaded twice if is it not fail to load. as your way i think it might be load twice.

phutaneVinayak avatar May 17 '19 12:05 phutaneVinayak

Mine was because I added the references under the wrong parent. It had the two "styles" and "script" nodes under different roots ("test", "build" among others) and I accidentally added it to the one under "test" root when it should be under "build".

More specifically, "projects"->"app-name"->"architect"->"build"->"options" instead of "projects"->"app-name"->"architect"->"test" (or something else) ->"options"

Angular Workspace Config link: https://angular.io/guide/workspace-config

savras avatar Jun 26 '19 18:06 savras

I also got this error when i included some js library which throw error on runtime (in browser).

My scripts section was like below.

              "node_modules/jquery/dist/jquery.min.js",
              "external/image-viewer/image-viewer.js",
              "external/dsp.js",
              "external/object-polyfills.js",
              "node_modules/slick-carousel/slick/slick.min.js",

And external/dsp.js file has some syntax error on runtime.

The reason it can be a problem is that Angular-cli compile all scripts js files to one file scripts.js. like this.

// jQuery minified code
// image-viewer code
// dsp.js code 
//object-polyfills.js code
// slick code 

If dsp.js code has some error, then browser will not run below code. It means if above script raise Exception, the code after Exception will not run.

So, it is not problem with slick-carousel. It depends on other library.

Two kind of things we can do

  1. Trying to find the root cause from other library.
  2. Change scripts define order of slick-carousel js file path in angular.json - scripts

leo6104 avatar Jul 29 '19 08:07 leo6104

$ npm install jquery --save $ npm install slick-carousel --save $ npm install ngx-slick-carousel --save

//angular.json

        "styles": [
          "src/styles.scss",
          "node_modules/slick-carousel/slick/slick.scss",
          "node_modules/slick-carousel/slick/slick-theme.scss"
        ],
        "scripts": [
          "node_modules/jquery/dist/jquery.min.js",
          "node_modules/slick-carousel/slick/slick.min.js"
        ],

and rebuild: ng serve

TheThai212 avatar Jun 11 '20 04:06 TheThai212

lay3tikom l3adfab lasswad hadchi makhdamch achinwa corona

mouadziani avatar Jul 20 '20 19:07 mouadziani

I have the same error with angular 9. From the stacktrace I think I see unslick called in the first place onDestroy, this in turns calls this.$instance.slick('unslick'); which is really weird cause there is a condition to check if there is an in instance in fact. I wonder if this runOutsideAngular doesn't lose context?

talamaska avatar Jul 30 '20 11:07 talamaska

changing "useDefineForClassFields": true tsconfig.json seems to have worked, apparently when importing the script the jQuery class is created using define, as such needing this change in tsConfig

gabrielsanmor avatar Mar 04 '24 19:03 gabrielsanmor