小编Chr*_*erg的帖子

库和角度的“不是构造函数”错误

我正在尝试为国际象棋游戏构建 Angular 应用程序。我想将 chess.js https://github.com/jhlywa/chess.js/blob/master/README.md库与https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master中声明的类型一起使用/types/chess.js 但每当我运行应用程序时,运行时就会出现错误。

我已经通过 npm 安装了软件包

npm install --save chess.js @types/chess.js
Run Code Online (Sandbox Code Playgroud)

在使用该库的服务中,我有以下相关代码:

import {Chess, ChessInstance} from "chess.js"

@Injectable({
  providedIn: 'root'
})
export class GameService {

 private chess: ChessInstance = new Chess()

 constructor() {}
 ...
}

Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

core.js:15724 ERROR Error: Uncaught (in promise): TypeError: chess_js__WEBPACK_IMPORTED_MODULE_4__.Chess is not a constructor
TypeError: chess_js__WEBPACK_IMPORTED_MODULE_4__.Chess is not a constructor
Run Code Online (Sandbox Code Playgroud)

我也尝试将其导入为:

import * as Chessjs from "chess.js"

@Injectable({
  providedIn: 'root'
})
export class GameService {

 private chess: Chessjs.ChessInstance …
Run Code Online (Sandbox Code Playgroud)

typescript angular

7
推荐指数
2
解决办法
4906
查看次数

标签 统计

angular ×1

typescript ×1