typescript 如何构建实用程序类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32790311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:47:37  来源:igfitidea点击:

How to structure utility class

typescripttypescript1.6

提问by Greg Gum

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

我有几个实用功能。打包这些然后导入它们的最佳方法是什么?

This is what I am trying to do:

这就是我想要做的:

import * as util from './util'

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

Then in the class:

然后在课堂上:

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

    doSomethingElse(val: string{ return val;}
}

The error message I get from VS is "Property doSomething does not exist on type util."

我从 VS 得到的错误消息是“类型 util 上不存在属性 doSomething”。

回答by k7sleeper

If you create a file utils.tswhich contains

如果您创建一个文件utils.ts,其中包含

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");
     }
}

回答by Asad Saeeduddin

There's a couple problems here:

这里有几个问题:

  1. You're not instantiating anything, and doSomethingis an instance method
  2. When you do import * as util, utilrepresents the module, not an object in it.
  1. 你没有实例化任何东西,它doSomething是一个实例方法
  2. 当你这样做时import * as utilutil代表模块,而不是其中的对象。

If you want Util, you should just import that:

如果你想要Util,你应该只导入:

import { Util } from './util'

Next, you should instantiate Util, before finally calling the method on it:

接下来,你应该实例化Util,最后调用它的方法之前:

var u = new Util();
u.doSomething("test");

Here's your code patched up:

这是您修补的代码:

import { Util } from './util'

export class MyClass{
     constructor()
     {
         var u = new Util();
         u.doSomething("test");
     }
}


All that said, there seems to be something odd about the way you're using your utils. This is totally personal opinion, but I wouldn't invoke methods that "do something", i.e. cause side effects, in a constructor.

尽管如此,您使用实用程序的方式似乎有些奇怪。这完全是个人意见,但我不会在构造函数中调用“做某事”的方法,即引起副作用。

Also, the methods in Utildon't really look like they need to be in that class, since the class holds no state that they depend on. You can always export regular functions from a module. If you wrote your utils module like this:

此外, 中的方法Util看起来并不需要在该类中,因为该类没有它们所依赖的状态。您始终可以从模块中导出常规函数。如果您像这样编写 utils 模块:

export function doSomething(val: string) { return val; }

export function doSomethingElse(val: string) { return val; }

you'd be exporting your functions directly and would sidestep the instantiation hassle, and in fact your original code would work correctly as is.

您将直接导出您的函数并避免实例化的麻烦,实际上您的原始代码将按原样正常工作。

回答by Renat Gatin

Alternative way:
1) Export constants in your utils.ts file:

替代方法:
1)在您的 utils.ts 文件中导出常量:

export const doSomething = (val: string): any => {
  return val;
};

export const doSomethingElse = (val: string): any => {
  return val;
};

2) Import and use this methods in main *.ts file:

2) 在主 *.ts 文件中导入并使用此方法:

import { doSomething, doSomethingElse } from './util';
...
let value1 = doSomething('abc');
let value2 = doSomethingElse ('efg');

回答by Max Rahder

Or you could export is as an object literal:

或者您可以导出为对象字面量:

export const Util = {
    doSomething(val: string){ return val;},
    doSomethingElse(val: string{ return val;}
}

回答by vijay

you can also create a util.tsclass which has a function to export

您还可以创建一个具有导出功能的util.ts

export const formatDateOfBirth = 
(dob: string) : string => `${dob.substring(0, 4)}-${dob.substring(4, 6)}-${dob.substring(6, 8)}`;

now you can import the method as below, shared folder structure is src > app > shared, i am calling this import inside src > app > shelf > shelf.component.tsfile

现在你可以导入如下方法,共享文件夹结构是src > app > shared,我在src > app >shelf >shelf.component.ts文件中调用这个导入

import { formatDateOfBirth } from '../shared/utils/util';
public getFormattedDate(dob: string):string{
  return formatDateOfBirth(dob);
}