简体   繁体   English

使用 TypeScript 中的导入类型在一行中调用导入的类中的方法

[英]Calling a method from an imported class using the Import Type in TypeScript in One Line

Looking for a simple one line solution to calling a method from a utility class in a typescript file that is being used in its respective html file being called in an attribute.寻找一种简单的单行解决方案来从 typescript 文件中的实用程序类调用方法,该文件在属性中调用的相应 html 文件中使用。

sample.html示例.html

<blah> class="{{getString(string)}}" </blah>

sample.ts样本.ts

import { BlahUtils } from 'blahUtils.ts';
/* This is the part in question */
getString(string).BlahUtils; or getString(string) = new BlahUtils;

blahUtils.ts blahUtils.ts

public static getString(string: string): string {
 return string.split('.').pop();
}

I saw an example before using a one line solution.在使用单行解决方案之前,我看到了一个示例。 I am aware of the other options but I am trying to find a shorter solution to shorten the code so I don't have to create a new function in all the other ts files I import the utility file into.我知道其他选项,但我正在尝试找到一种更短的解​​决方案来缩短代码,因此我不必在我将实用程序文件导入到的所有其他 ts 文件中创建新函数。 I know there is a way to do it this way and am looking for someone who might have the answer.我知道有一种方法可以做到这一点,并且正在寻找可能有答案的人。

Right now the best solution I have is现在我最好的解决方案是

getString = BlahUtils.getString;
getString(string);

Is there a way to do it in one line of code?有没有办法在一行代码中做到这一点?

Actually found a solution.其实找到了解决办法。

In the sample.ts file I am able to simply say在 sample.ts 文件中,我可以简单地说

getString = BlahUtils.getString;

and that allows me to call the method directly from the HTML Attribute in the html file.这允许我直接从 html 文件中的 HTML 属性调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM