在Angular 2中使用具有out type defination的Node模块

时间:2017-03-22 04:52:02

标签: node.js angular typescript node-modules

我编写了一个非常简单的节点模块[npm install twinconsole]并发布到npm。

我还包括浏览器build(umd模块),以便它可以 也可以从浏览器中使用。

以下是节点模块相关代码

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

现在我想在我的Angular 2打字稿应用程序中使用这个节点模块来做那个

  1. 我在index.html中包含了以下CDN文件。

    < script src =" https://npmcdn.com/twinconsole@1.2.1/dist/index.umd.min.js">

  2. 我需要做什么才能在节点模块中导出的print()函数可以在下面的根组件中使用

    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!';
    }
    

1 个答案:

答案 0 :(得分:1)

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!';
}