refactor
refactor

Reputation: 15094

Using Node module with out type defination in Angular 2

I have written a very simple node module [ npm install twinconsole ] and published to npm.

I also included browser build ( umd module ) so that it can be used from browser as well.

Below is node module related code

module.exports.print = msg => {
console.log(msg);
}

Now I would like to use this node module from my Angular 2 typescript application, to do that

  1. I have included below CDN file in index.html.

    < script src="https://npmcdn.com/[email protected]/dist/index.umd.min.js">

What is that I need to do so that print() function exported in node module can be used in below root component

import { Component } from '@angular/core';

declare var twinconsole: any;  // ADDEED THIS LINE , IS THIS CORRECT ?


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  title = 'app works!';
}

Upvotes: 0

Views: 74

Answers (1)

wickdninja
wickdninja

Reputation: 1039

import { Component } from '@angular/core';

//define interface to get "intellisense" aka autocomplete and type errors
interface TwinConsole{
  print(msg:string);
}

declare var twinconsole: TwinConsole;


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  title = 'app works!';
}

Upvotes: 1

Related Questions