[英]Can I import functions into a typescript class file?
I have Typescript classes that look like this: 我有这样的Typescript类:
class WordService implements IWordService {
wordCreatedById: number = 0;
wordModifiedById: number = 0;
static $inject = [
"$http",
"$q",
...
];
constructor(
public $http: ng.IHttpService,
public $q: ng.IQService,
...
) {
}
wordClear = (): void => {
this.word = null;
this.wordBase = null;
}
...
}
My class files have become very long now as I defined more and more functions like wordClear. 我的类文件已经变得很长了,因为我定义了越来越多的函数,比如wordClear。
Is there any way that I could move functions into another file and import them into my class? 有什么方法可以将函数移动到另一个文件并导入到我的类中?
If you're asking about using other functions in class definitions this is one way to accomplish it. 如果您要求在类定义中使用其他函数,这是实现它的一种方法。 Let's put this in utilFunctions.ts (or whatever you want to call it. 我们把它放在utilFunctions.ts(或者你想要的任何东西)中。
export function wordClear(obj:{word:any, wordBase:any}/*replace with relevant interface*/) :void {
obj.word = null;
obj.wordBase = null;
}
and then in the class ts file 然后在类ts文件中
import * as utilFunctions from './utilFunctions'
class WordService implements IWordService {
...
wordClear = ()=>utilFunctions.wordClear(this);
}
Or 要么
import * as utilFunctions from './utilFunctions'
class WordService implements IWordService {
...
public wordClear() {
utilFunctions.wordClear(this);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.