【问题标题】:Exporting a function on typescript "declaration or statement expected"在打字稿“声明或声明预期”上导出函数
【发布时间】:2017-02-02 02:52:07
【问题描述】:

我意识到这真的很简单,但打字稿在过去几年似乎发生了很大变化,我只是无法通过我在堆栈溢出中找到的先前答案来完成此操作。

let myfunction = something that returns a function

export myfunction;

我收到错误“声明或声明”

如何从一个非常简单的 ts 文件中导出一个函数,以便能够在另一个 ts 文件中使用该函数?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    好像

    let myfunction = something that returns a function
    export {myfunction};
    

    会成功的。

    【讨论】:

      【解决方案2】:

      使用

      export default myfunction
      

      如果你只有这个函数可以从这个文件中导出。否则使用

      export { myfunction, <other exports> }
      

      导出myfunction 以及要导出的其他类型

      【讨论】:

        【解决方案3】:

        您可以使用模块化顶级importexport 声明从另一个文件调用function 或实例化class

        file1.ts

        // This file is an external module because it contains a top-level 'export'
        export function foo() {
            console.log('hello');
        }
        export class bar { }
        

        file2.ts

        // This file is also an external module because it has an 'import' declaration
        import f1 = module('file1');
        f1.foo();
        var b = new f1.bar();
        

        【讨论】:

        • 谢谢!如果我不能当场声明函数,这将如何工作?在我的情况下,我想导出的函数是通过运行另一个函数返回的。
        猜你喜欢
        • 1970-01-01
        • 2021-02-09
        • 2018-06-12
        • 2016-03-27
        • 1970-01-01
        • 2019-01-19
        • 1970-01-01
        • 2020-09-12
        • 1970-01-01
        相关资源
        最近更新 更多