如何从 Fable 中的 NPM 模块导入函数?
How to import functions from NPM modules in Fable?
我正在尝试在 Fable 项目中使用 NPM 包 normalize-wheel。
我确认包已经下载到 node-modules
文件夹。
与 documentation and this article 一致,我使用以下 F# 代码:
type INormalizedWheel =
abstract member pixelX: float
abstract member pixelY: float
abstract member spinX: float
abstract member spinY: float
[<Import("normalizeWheel", "normalize-wheel")>]
let normalizeWheel (we: React.WheelEvent) : INormalizedWheel = jsNative
...
let x = normalizeWheel wheelEvent
但是,每当到达最后一行时,都会抛出 JS 错误消息 "Object(...) is not a function"
。在调试器中,normalizeWheel
的类型显示为 undefined
.
我想导入不起作用,但我做错了什么?
我们来看看normalize-wheel/index.js
module.exports = require('./src/normalizeWheel.js');
这告诉我们没有任何名为 normalizeWheel 的函数(否则它将是 module.exports.normalizeWheel = ...)。这意味着我们需要导入默认函数。可以这样做:
[<Import("default", "normalize-wheel")>]
let normalizeWheel (we: React.WheelEvent) : INormalizedWheel = jsNative
// Or
let normalizeWheel (we: React.WheelEvent) : INormalizedWheel = importDefault "normalize-wheel"
我正在尝试在 Fable 项目中使用 NPM 包 normalize-wheel。
我确认包已经下载到 node-modules
文件夹。
与 documentation and this article 一致,我使用以下 F# 代码:
type INormalizedWheel =
abstract member pixelX: float
abstract member pixelY: float
abstract member spinX: float
abstract member spinY: float
[<Import("normalizeWheel", "normalize-wheel")>]
let normalizeWheel (we: React.WheelEvent) : INormalizedWheel = jsNative
...
let x = normalizeWheel wheelEvent
但是,每当到达最后一行时,都会抛出 JS 错误消息 "Object(...) is not a function"
。在调试器中,normalizeWheel
的类型显示为 undefined
.
我想导入不起作用,但我做错了什么?
我们来看看normalize-wheel/index.js
module.exports = require('./src/normalizeWheel.js');
这告诉我们没有任何名为 normalizeWheel 的函数(否则它将是 module.exports.normalizeWheel = ...)。这意味着我们需要导入默认函数。可以这样做:
[<Import("default", "normalize-wheel")>]
let normalizeWheel (we: React.WheelEvent) : INormalizedWheel = jsNative
// Or
let normalizeWheel (we: React.WheelEvent) : INormalizedWheel = importDefault "normalize-wheel"