Accessing $ (JQuery) in ng2 service
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
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.
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?
Try to install Jquery via npm
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?
- Install the package via npm
- Add it to the imports in webpack.config.vendor.js
- 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.
Current sigbalR client is not support server side processing. And more over signalR support for aspnet core is not good. So answer probably no.
Which technology would you suggest to use instead of signalr for a ng2 application?
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!
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)....
- Install the package via npm
- Add it to the imports in webpack.config.vendor.js
- include it in the component
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 :)
Thanks @laskoviymishka and @MarkPieszak for providing the info!