JavaScriptServices icon indicating copy to clipboard operation
JavaScriptServices copied to clipboard

Accessing $ (JQuery) in ng2 service

Open bencameron00 opened this issue 10 years ago • 10 comments

Greater starter pack!

I am trying to connect to a signalr hub from a service using this starter but cannot access $ and getting...

Exception: Call to Node module failed with error: Error: Uncaught (in promise): ReferenceError: $ is not defined.

My service uses these 2 lines...

declare var $; this.connection = $.hubConnection('http://localhost:5000/' + 'signalr/');

Should jQuery be mapped correctly and be accessible like this?

I have a copy of the code at https://github.com/bencameron00/Ng2JQueryHub

Thanks Ben

bencameron00 avatar Nov 03 '16 11:11 bencameron00

You have to import your $ variable.

Like

import * as $ from "jquery";

But keep in mind that jQuery highly coupled with DOM and will not work for server side render (most likely signalR client also will no work). I would suggest for you to create some stub singnalR service that will be used on server to prevent any errors.

laskoviymishka avatar Nov 03 '16 12:11 laskoviymishka

Thanks for the feedback and advice. After adding the above line I now see an error saying...

error Cannot find module 'jquery'

Any other ideas?

bencameron00 avatar Nov 03 '16 14:11 bencameron00

Try to install Jquery via npm

laskoviymishka avatar Nov 03 '16 16:11 laskoviymishka

Ahh yes, thank you. I'm still learning about the setup here. So what would the steps be to include a third party script like signalr?

  1. Install the package via npm
  2. Add it to the imports in webpack.config.vendor.js
  3. include it in the component

Am I missing anything as this doesn't quite work for me? Does any other setup need to be changed? Are there any plans to add signalr to the template?

I really appreciate your help, thank you.

bencameron00 avatar Nov 04 '16 14:11 bencameron00

Current sigbalR client is not support server side processing. And more over signalR support for aspnet core is not good. So answer probably no.

laskoviymishka avatar Nov 04 '16 16:11 laskoviymishka

Which technology would you suggest to use instead of signalr for a ng2 application?

bencameron00 avatar Nov 04 '16 16:11 bencameron00

I think what you might need to do is not use any websockets during the server-side rendering process. What kinds of things are you doing with websockets, notifications / etc?

You can use isBrowser or isNode to make conditional code statements.

So in your NgModule or a component you can do:

 import { isBrowser, isNode } from 'angular2-universal';

Then use them in if (isBrowser) { /* do socket stuff */ } statements, to use websockets or not.

When providing Modules or Imports (to your App's main NgModule you could do the same thing)

 isBrowser ? SomeBrowserModule : SomeServerModule

In the future we'll be looking to make things like this a little easier/smoother as well hopefully!

MarkPieszak avatar Nov 04 '16 18:11 MarkPieszak

I see, that makes sense. I can definitely use those properties. I need to update a chart using data pushed from the server. Using your above idea I could use a sigr service whenisBrowser or use a different service that does not use sigr when isnode. Maybe the second could have some sort of polling mechanism.

So to recap, if I wanted to add a third party script, signalr in this case, to the current starter pack how would I go about this? Is it something like these steps (from above)....

  1. Install the package via npm
  2. Add it to the imports in webpack.config.vendor.js
  3. include it in the component

bencameron00 avatar Nov 05 '16 08:11 bencameron00

Yes, that's the general workflow that should work! Just make sure to be careful it doesn't run on the server (during server-side rendering) and you should be all set :)

MarkPieszak avatar Nov 05 '16 15:11 MarkPieszak

Thanks @laskoviymishka and @MarkPieszak for providing the info!

SteveSandersonMS avatar Nov 07 '16 17:11 SteveSandersonMS