【问题标题】:angular waypoints typescript error module not found找不到角度航点打字稿错误模块
【发布时间】:2019-05-20 03:59:51
【问题描述】:

我使用 npm install waypoints 安装了航点库并使用

添加了类型

npm install --save @types/waypoints

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

Using waypoints.js in Angular 4 application

讨论了同样的问题。用户有同样的错误。 一条评论建议“使用 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 () {

  },
});

我得到另一个错误 ->

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

【问题讨论】:

  • 可能Waypoint 导入路径错误。您应该导入 Waypoint 模块,而不是类型定义。不要太相信自动导入
  • 你能解决这个问题吗?

标签: angular typescript types npm-install waypoint


【解决方案1】:

出口

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 类型就可以了。

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2014-11-21
    相关资源
    最近更新 更多