How to connect a third party library to Angular 4 for use as a service

I'm trying to add collect.js to my Angular 4 app, built with Angular CLI, which has this native typing file, but I don't understand how to import the package and start using it. Below are the imports to get me to export the package, but I don't understand how to access them:

import * as Collection from 'collect.js';

      

This is the file to import collect.js

:

function Collection(collection) {
  this.items = collection || [];
}

Collection.prototype[Symbol.iterator] = require('./methods/symbol.iterator');

Collection.prototype.all = require('./methods/all');
// ... removed for brevity
Collection.prototype.zip = require('./methods/zip');

module.exports = collection => new Collection(collection);
module.exports.default = collection => new Collection(collection);

      

The import collection is the method you would expect from exporting a module, but I cannot call it without VSCode, stating that it has no call signature and I am also not sure if the typing is loaded automatically or if I need to add them manually. I was hoping to make a service out of it by importing it and then just the mapping methods and then providing that in the CoreModule so that I can use DI and use it wherever I want in the application, but I'm not sure how to access the API so that make it a service.

+3


source to share


1 answer


If you want to add a third party library to your Angular app and you are using the Angular CLI then follow the instructions in the link below.

https://github.com/angular/angular-cli/wiki/stories-third-party-lib

Below is a summary of the steps

Install the library and library typing using npm

.

npm install <library-name> --save
npm install @types/<library-name> --save-dev

      



Then add the library to the type array src/tsconfig.app.json

"types":[
  "<library-name>"
]

      

Use the library in your code as usual.

If you have any problems, please refer to the link above.

+3


source







All Articles