我正在学习角度2,我已经为我想要在我的一个服务中使用的truncate方法编写了一个ts定义。
truncate.ts
interface String {
truncate(max: number, decorator: string): string;
}
String.prototype.truncate = function(max, decorator){
decorator = decorator || '...';
return (this.length > max ? this.substring(0,max)+decorator : this);
};
如何将其导入另一个打字稿模块或至少使其可供全局使用。
答案 0 :(得分:11)
如何将其导入另一个打字稿模块或至少使其可以全局使用。
将其移至文件stringExtenions.ts
:
interface String {
truncate(max: number, decorator: string): string;
}
String.prototype.truncate = function(max, decorator){
decorator = decorator || '...';
return (this.length > max ? this.substring(0,max)+decorator : this);
};
导入文件,如:
import "./stringExtensions"
https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html
答案 1 :(得分:6)
在Ionic 3 Angular 4应用程序中使用typescript 2.3.4我创建了一个名为stringExtensions.ts的文件并将其放入其中
foo()
答案 2 :(得分:4)
在我的TypeScript 2.3.4中,使用
declare global {
interface Date {
format(format: string): string;
}
}
TypeScript文档Augmenting global/module scope from modules