2

I am trying to include an external JS library in my Angular 2 app and trying to make all the methods in that JS file as a service in Angular 2 app.

For eg: lets say my JS file contains.

var hello = {
   helloworld : function(){
       console.log('helloworld');
   },
   gmorning : function(){
      console.log('good morning'); 
   }
}

So I am trying to use this JS file and reuse all the methods in this object and add it to a service, so that my service has public methods, which in turn calls this JS methods. I am trying to reuse the code, without reimplementing all the methods in my typescript based Angular 2 app. I am dependent on an external library, which I cant modify. Please help, thank you in advance.

2
  • What do you to implement your application: es5 or es6? which a module loader if any? Commented Apr 22, 2016 at 13:23
  • ES6 and i am using system.js as module loader Commented Apr 22, 2016 at 13:34

2 Answers 2

1

With ES6, you could export your variable:

export var hello = {
  (...)
};

and import it like this into another module:

import {hello} from './hello-module';

assuming that the first module is located into the hello-module.js file and in the same folder than the second one. It's not necessary to have them in the same folder (you can do something like that: import {hello} from '../folder/hello-module';). What is important is that the folder is correctly handled by SystemJS (for example with the configuration in the packages block).

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

4 Comments

Hi thiery, what if i want to do the same thing in es5? without using the export..that is what if i am dependent on external libraries, which i cant modify?
Otherwise you include the file within a script tag but your hello variable will global :-(. Most of external libraries support a module manager like commonjs or systemjs. Notice that you can configure a commonjs-compliant module into systemjs. Which external libraries do you want to use?
I seems that strophe.js supports amd for module loader (which is supported by systemsjs). You could try to use this configuration: System.config({ map: { 'strophe.js': 'node_modules/strophe.js/strophe.js' }});
This way you will be able to import it this way: import {strophe} from 'strophe.js';
1

When using external libs which are loaded into the browser externally (e.g. by the index.html) you just need to say your services/component that it is defined via "declare" and then just use it. For example I recently used socket.io in my angular2 component:

import { Component, Input, Observable, AfterContentInit } from angular2/angular2';
import { Http } from 'angular2/http';

//needed to use socket.io! io is globally known by the browser!
declare var io:any;

@Component({
  selector: 'my-weather-cmp',
  template: `...`
})
export class WeatherComp implements AfterContentInit{
  //the socket.io connection
  public weather:any;
  //the temperature stream as Observable
  public temperature:Observable<number>;

    //@Input() isn't set yet
    constructor(public http: Http) {
      const BASE_URL = 'ws://'+location.hostname+':'+location.port;
      this.weather = io(BASE_URL+'/weather');
      //log any messages from the message event of socket.io
      this.weather.on('message', (data:any) =>{
        console.log(data);
      });
    }

    //@Input() is set now!
    ngAfterContentInit():void {
      //add Observable
      this.temperature = Observable.fromEvent(this.weather, this.city);
    }
}

2 Comments

This comment to a similiar question gives the best answer: stackoverflow.com/a/35799163/5271798
thank you for the answer..i tried a similar approach and it worked. But concern is the socket.io is globally known :(

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.