代码之家  ›  专栏  ›  技术社区  ›  Chris Knight

无法导入模块

  •  1
  • Chris Knight  · 技术社区  · 8 年前

    我有以下代码:

    import {AsyncLock} from 'async-lock';
    

    [ts]模块“my\u app/node\u modules/@types/async lock/index”没有导出的成员“AsyncLock”。

    但是如果我看 my_app/node_modules/@types/async-lock/index.d.ts

    interface AsyncLockDoneCallback {
        (err?: Error, ret?: any): void;
    }
    
    interface AsyncLockOptions {
        timeout?: number;
        maxPending?: number;
        domainReentrant?: boolean;
        Promise?: any;
    }
    
    declare class AsyncLock {
        constructor(options?: AsyncLockOptions);
    
        acquire(key: string | string[], fn: (done: AsyncLockDoneCallback) 
    => any, cb: AsyncLockDoneCallback, opts?: AsyncLockOptions): void;
        acquire(key: string | string[], fn: (done: AsyncLockDoneCallback) 
    => any, opts?: AsyncLockOptions): PromiseLike<any>;
    
        isBusy(): boolean;
    }
    
    declare namespace AsyncLock { }
    
    export = AsyncLock;
    

    在我看来,这里似乎正在导出异步锁。我的导入定义哪里出错了?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Community Mohan Dere    6 年前

    简短的回答

    当声明文件像这样导出时:

    export = AsyncLock;
    

    然后我们通过以下两种方式之一导入:

    import AsyncLock = require("async-lock"); // thank you unional
    import * as AsyncLock from "async-lock";
    

    更多详细信息

    TypeScript modules documentation

    CommonJS和AMD通常都有导出对象的概念,该对象包含来自模块的所有导出。

    export { AsyncLock }
    

    在这种情况下,我们会这样导入它:

    import { AsyncLock } from "async-lock";
    
    推荐文章