使用基于Typescript的Angular2项目的javascript库

时间:2023-01-01 00:27:07

I'm trying to create a proof-of-concept app with Angular 2 and Typescript. I'm wanting to incorporate the toastr notification library and was hoping there would be a simple way to get it running without the definition files.

我正在尝试使用Angular 2和Typescript创建一个概念验证应用程序。我想要合并toastr通知库,并希望有一种简单的方法让它在没有定义文件的情况下运行。

I've created a file in my common services directory called toastr.d.ts]:

我在公共服务目录中创建了一个名为toastr.d.ts的文件:

  declare module "toastr" {
//   var noTypeInfoYet: any; 
//   export = noTypeInfoYet;
}

which is called from the service common.ts

这是从服务common.ts调用的

 /// <reference path="toastr.d.ts" />
import {Injectable} from 'angular2/core';
import {toastr} from 'toastr';

@Injectable()
export class Common {

  constructor() {}
  
  log(message:string){
      toastr(message);
      console.log(message);
  }

}

however I get the compilation error: [DiffingTSCompiler]: Typescript found the following errors: app/services/common/common.ts (3,9): Module '"toastr"' has no exported member 'toastr'.

但是我得到了编译错误:[DiffingTSCompiler]:Typescript发现了以下错误:app / services / common / common.ts(3,9):模块'“toastr”'没有导出成员'toastr'。

All of the code can be found on Cloud 9

所有代码都可以在Cloud 9上找到

1 个解决方案

#1


1  

The following:

下列:

import {toastr} from 'toastr';

is wrong. toastr doesn't export a variable toastr in fact the root level export IS toastr. Fix:

是错的。 toastr实际上没有导出变量toastr根级导出IS toastr。固定:

import * as toastr from 'toastr';

#1


1  

The following:

下列:

import {toastr} from 'toastr';

is wrong. toastr doesn't export a variable toastr in fact the root level export IS toastr. Fix:

是错的。 toastr实际上没有导出变量toastr根级导出IS toastr。固定:

import * as toastr from 'toastr';