在我的/ext/app-entension.ts
(“类型记录”:"^2.4.2")中,我声明了App
(离子角)类的扩展函数:
// **error #1** from the list below
import { NavController, App } from "ionic-angular";
interface App {
getMyCoolNav(): NavController;
}
// **error #2** from the list below
App.prototype.getMyCoolNav = function(this: App): NavController {
// logic to define suitable nav combining the following calls
// this.getRootNavs && this.getActiveNavs
// **error #3** from the list below
}
在我的页面中,我可以导入这个扩展并使用它,没有任何问题:
import { NavController, App } from "ionic-angular";
import '../ext/app-extensions'
// **error #4** from the list below
this.app.getMyCoolNav().push(MyCoolPage);
它在运行时运行良好,而VS代码/类型记录让我很难处理各种错误:
this
用法:在'App‘类型上不存在ts属性'getRootNavs’。我的扩展方法注册可能有什么问题?
发布于 2018-10-09 01:25:16
您需要使用接口扩展模块,以便告诉编译器有关新接口的信息。例如:
文件test.ts
export class Test {
getSomething(): string {
return "something";
}
}
文件testExtensions.ts
import { Test } from "./test";
declare module "./test" {
interface Test {
getSomethingElse(): string;
}
}
Test.prototype.getSomethingElse = function(): string {
return "somethingElse";
}
这确实告诉了编译器有关接口的信息,您应该能够根据需要扩展原型。参见https://www.typescriptlang.org/docs/handbook/declaration-merging.html,搜索“模块增强”
https://stackoverflow.com/questions/52709589
复制相似问题