英文:
SAPUI5 TypeScript Error: Extending @sapui5/types Component Interface
问题
这个问题与TypeScript有关,但我认为寻求SAPUI5社区的帮助可能会导致解决方案。
我正在处理一个带有TypeScript的SAPUI5项目,并且已将@sapui5/types
包安装为开发依赖项。我在我的Component.ts
文件中定义了一个名为getContentDensityClass()
的函数,如下所示:
// webapp/Component.ts
public getContentDensityClass() {
// ...
return this._sContentDensityClass;
}
在我的App.controller.ts
中,我想像这样使用这个函数:
// webapp/controller/App.controller.ts
public onMessagePopoverPress(oEvent: Event) {
// ...
syncStyleClass(this.getView()
.getController()
.getOwnerComponent()
.getContentDensityClass(), // 这里出错了
this.getView(),
oMessagePopover);
// ...
}
然而,TypeScript引发了以下错误:
getContentDensityClass在类型为Component的getOwnerComponent上不存在
为了解决这个问题,我尝试扩展来自@sapui5/types
的Component类型,如下所示:
declare module "@sapui5/types" {
export interface Component {
getContentDensityClass(): string;
}
}
尽管尝试了这种方法,错误仍然存在。
英文:
This issue is related to TypeScript, but I believe seeking help from the SAPUI5 community might lead to a solution.
I'm working on a SAPUI5 project with Typescript, and I have the @sapui5/types
package installed as a dev dependency. I have a function called getContentDensityClass()
defined in my Component.ts
file as follows:
// webapp/Component.ts
public getContentDensityClass() {
// ...
return this._sContentDensityClass;
}
In my App.controller.ts
, I want to use this function like this:
// webapp/controller/App.controller.ts
public onMessagePopoverPress(oEvent: Event) {
// ...
syncStyleClass(this.getView()
.getController()
.getOwnerComponent()
.getContentDensityClass(), // here is the error
this.getView(),
oMessagePopover);
// ...
}
However, TypeScript raises the following error:
getContentDensityClass does not exist on getOwnerComponent of type Component
To address this issue, I tried extending the Component type from @sapui5/types as shown below:
declare module "@sapui5/types" {
export interface Component {
getContentDensityClass(): string;
}
}
Despite this attempt, the error persists.
答案1
得分: 1
getOwnerComponent
返回通用的UI5组件类。然而,您自定义的方法 getContentDensityClass
是在您自定义的组件类中定义的。
您应该断言 getOwnerComponent
的类型,例如:
import AppComponent from "../Component" // 指向您的 Component.ts 的路径;
...
(this.getView()
.getController()
.getOwnerComponent() as AppComponent)
.getContentDensityClass()
如果您不想每次都这样做,我建议您将这个代码放在您的 BaseController 中:
import AppComponent from "../Component";
getOwnerComponent(): AppComponent {
return super.getOwnerComponent() as AppComponent;
}
然后,您的原始代码应该可以正常运行,无需进行任何修改。
英文:
getOwnerComponent
returns the generic UI5 component class. However, your custom method getContentDensityClass
is defined in your custom component class.
You should assert the type of getOwnerComponent
, for example:
import AppComponent from "../Component" // path to your Component.ts;
...
(this.getView()
.getController()
.getOwnerComponent() as AppComponent)
.getContentDensityClass()
If you don't want to do this every time I'd suggest you put this in your BaseController
import AppComponent from "../Component";
getOwnerComponent(): AppComponent {
return super.getOwnerComponent() as AppComponent;
}
Then your original code should work without any modification.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论