1

我使用 npm install waypoints 安装了航点库并添加了类型

npm install --save @types/waypoints.

我收到错误 /node_modules/@types/waypoints/index.d.ts' is not a module。

在 Angular 4 应用程序中使用 waypoints.js

讨论了同样的问题。用户有同样的错误。一条评论建议“使用 export package_name = package_name。这应该可以”我不知道如何实现。

我试图添加

export = waypoints
export as namespace waypoints;
declare namespace waypoints { 

}

import * as Waypoint from 'waypoints';

然后,当我尝试导入航点时,我没有收到任何错误。但如果我再添加一个像这样的航点:

let waypoint = new Waypoint.Waypoint({
  element: document.querySelector("#p5"),
  handler: function () {

  },
});

我得到另一个错误->

未找到模块:错误:无法解析“航点”

4

1 回答 1

0

出口

export = waypoints
export as namespace waypoints;
declare namespace waypoints { 

}

不会解决问题。不要更改 @types/waypoints 中的任何内容,只需将其添加declare const Waypoint: any;到您需要的组件中即可。像简单的例子

declare const Waypoint: any; // Declare the Waypoint class here to access it

@Component({
  selector: 'my-comp',
  template: '<div id="abc">Abc</div>',
})
export class MyComponent implements OnInit {
  ngOnInit() {
    let wp = new Waypoint({
      element: document.getElementById("abc"),
      handler: (direction) => {
        if (direction == 'down') {
          wp.element.classList.add("fadeInUp");
          wp.destroy();
        }
      },
      offset: '95%'
    });
  }
}

这是解决编译错误。正如我们所知,Waypoint一旦应用程序运行,它就会出现,所以只需 将Waypoint类声明为类型any就可以了。

于 2019-04-10T07:53:02.923 回答