如何构建实用程序类 [英] How to structure utility class

查看:41
本文介绍了如何构建实用程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个实用函数.将它们打包然后导入的最佳方法是什么?

I have several utility functions. What is the best way to package these up, and then import them?

这就是我想要做的:

import * as util from './util'

export class myClass{
     constructor()
     {
           util.doSomething("test");
     }
}

然后在课堂上:

export class Util{
    doSomething(val: string){ return val;}

    doSomethingElse(val: string{ return val;}
}

我从 VS 得到的错误信息是:

The error message I get from VS is:

属性 doSomething 在类型 util 上不存在.

推荐答案

如果你创建了一个包含

export default class Utils {
    static doSomething(val: string) { return val; }
    static doSomethingElse(val: string) { return val; }
}

然后你可以像这样简化你的客户端代码:

then you can simplify your client code like this:

import Utils from './utils'

export class MyClass {
     constructor()
     {
         Utils.doSomething("test");
     }
}

这篇关于如何构建实用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆