0

I've to do some initialization for my theme after the view has been initialized. I've implemented AfterViewInit, I've managed to import jquery:

import * as $ from 'jquery';

But now I need to execute this:

ngAfterViewInit() {
    $(document).trigger('nifty.ready');
}

And I've trouble, because it seems that at this point, document is not known. I guess I should have another import, but I can't find from where? My whole app.component.ts:

import { Component, AfterViewInit } from '@angular/core';
import * as $ from 'jquery';

@Component({
    selector: 'app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
    ngAfterViewInit() {
        $(document).trigger('nifty.ready');
    }
}

The error I get:

Exception: Call to Node module failed with error: TypeError: Cannot read property 'document' of undefined at module.exports.module.exports (E:\My\Clients\AppClientApp\dist\vendor.js:12879:12) at AppComponent.ngAfterViewInit

EDIT

You can find here the whole web app part.

EDIT2 In fact I've the impression that jquery is not initialized properly at all, once loaded(without error), window.jquery return undefined... any idea why?

10
  • Post your full code, looks like you are using document in a node module which does not exist Commented Apr 14, 2017 at 6:43
  • @Dummy : If I remove the $(document)... line, everything is working. I added the whole class Commented Apr 14, 2017 at 6:47
  • Try import $ from 'jquery'; I use document just fine inside ngOnInit in my app Commented Apr 14, 2017 at 6:49
  • "document" reference works fine in a typescript application. Did you try to rename $ -> J Commented Apr 14, 2017 at 6:50
  • @Dummy, I tried, but I still get the Call to Node module failed with error: ReferenceError: document is not defined Commented Apr 14, 2017 at 6:52

3 Answers 3

1

You can inject the document in your component.

https://angular.io/docs/ts/latest/api/platform-browser/index/DOCUMENT-let.html

import { Component, AfterViewInit, Inject } from '@angular/core';
import {DOCUMENT} from "@angular/platform-browser";

@Component({
  selector: 'app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  constructor(@Inject(DOCUMENT) private document) {
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think this is a step forward, now I've another error: Call to Node module failed with error: Error: jQuery requires a window with a document
I have the same error: "jQuery requires a window with a document". Has anyone figured out how to address this error? I got this far using import * as $ from 'jquery' in the app.component.ts file, but do not know how to get past this.
0

Instead of importing jquery try this -

declare var jQuery:any;

jQuery(document);

console.log jQuery(document) to be sure

P.S make sure jquery is available on the window scope!

5 Comments

I get some Exception: Call to Node module failed with error: ReferenceError: jQuery is not defined now. What do you mean by available on the window scope? How do I do that?
How are you installing jquery and how does your build system look like?
Most probably I think you are getting the error because jquery is not available on window scope
jquery is installed through NPM, and included within the webpack vendor list
You can find the webpack.vendor.js here: github.com/jgrossrieder/Astro/blob/master/Clients/… This is my actual version, I just commented the call to jquery.
0

I took a look to your project. Well, I didn't test all the .Net features, but I guess your webpack config misses a plugin provider for jQuery (used to be done by angular-cli).

So you ever done :

npm install jquery --save

Try this :

npm install @types/jquery --save-dev

And then add the jquery plugin provider to your webpack.config.js :

  plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery'
      })
    ]

Finally import it as follow :

import "jquery";
declare var $: any;

4 Comments

I'm not sure to understand, the jquery seems to be listed as a npm dependency already, no? and I just saw now there a jquery provide plugin in webpack.config.vendor.js: new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
Right. I didn't see it. So you may have jquery as a global variable. Try to remove your import * as $ from 'jquery'; and eventually check your are not installing 2 jquery packages...
if I remove it, I've this: Exception: Call to Node module failed with error: ReferenceError: jquery is not defined
Ok just try another suggestion that works for me (I edited my answer)

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.