javascript - 动态更改 SAP Fiori 标题时出现 "this.getService is not a function"

标签 javascript sapui5 sap-fiori

我正在尝试动态更改 SAP Fiori 标题,即在启动板启动后并单击我选择的应用程序后,应用程序的标题应根据我的需要进行更改。 我正在使用此文档页面作为指南 https://ui5.sap.com/1.54.3/docs/api//symbols/sap.ushell.ui5service.ShellUIService.html .

在我的 webapp/Component.js 中,我有我的 setAppTitle() 方法,它应该为应用程序设置一个新的自定义标题,并从 中调用>onAfterRendering() webapp/view/S2Custom.controller.js 方法。

我不能直接从 webapp/Component.jsinit() 方法中调用 setAppTitle() 的原因是因为我从 webapp/i18n/i18n.properties 获取我的标题,它在 Component.jsinit() 期间尚未“加载” >.

为了避免在 Component.js 中使用类似 setTimeout(() => this.setAppTitle(sTitle), 6000) 的东西,我决定在 中调用它webapp/view/S2Custom.controller.js 的 code>onAfterRendering() 方法。

webapp/view/S2Custom.controller.js

onAfterRendering: function (oEvent) {
  var sComponentId = sap.ui.core.Component.getOwnerIdFor(this.getView());
  var oComponent = sap.ui.component(sComponentId);
  var i18nModel = new sap.ui.model.resource.ResourceModel({
    bundleName: "ui.s2p.srm.sc.blahblah.BlahBlahExtension.i18n.i18n"
  });
  var oResource = i18nModel.getResourceBundle();
  var sTitle = oComponent.getModel("appModel").getProperty("/sMode") === "OUTBOX" ? oResource.getText("APP_ONE") : oResource.getText("APP_TWO");
  this.getOwnerComponent().setAppTitle(sTitle);
},

webapp/Component.js

setAppTitle: function (sText) {
  var sTitle = sText;
  console.log("TEST: ", this.getService("ShellUIService")); // ERROR !!!
  try {
    this.getService("ShellUIService").then(function (oService) {
      oService.setTitle(sTitle);
    }, function (oError) {
      jQuery.sap.log.error("Cannot get ShellUIService");
    });
  } catch (err) {
    console.log("TEST - ERROR: ", err);
  }
},

setAppTitle() 方法中的问题 - 控制台显示错误:

this.getService is not a function.

所以我进入了我的manifest.json

"sap.ui5": {
  "_version": "1.2.0",
  "services": {
    "ShellUIService": {
      "factoryName": "sap.ushell.ui5service.ShellUIService"
    }
  },
  "dependencies": {
    "minUI5Version": "1.28.5",
    "libs": {}
  },

我注意到 Web IDE 在包含 "services": { 的行上显示 Property "services"is not allowed 错误。

你能告诉我是什么导致了这个问题吗?我已经尝试在 Component.jsinit() 方法中单独调用 this.getService("ShellUIService") ,不幸的是同样的错误

this.getService is not a function.

最佳答案

您必须使用旧的 UI5 版本。 API component.getService仅从 1.37.0 开始可用:

sapui5 sap.ui.core.Component#getService

您可以在应用程序中按 Ctrl+Left Alt+Shift+P 查看当前版本.


关于 Web IDE 错误信息..

I noticed Web IDE showed Property "services" is not allowed error on line which contains "services": {.

  1. 如果可能,将 UI5 资源更新为 currently supported versions 之一.
  2. 除了根 _version,删除 sap.appsap.ui5 中的所有其他 _version等..
  3. 根据表 AppDescriptor Release and SAPUI5 Version 将根 _version 设置为高于 1.3.0 的值.
  4. 重新加载 Web IDE。

关于javascript - 动态更改 SAP Fiori 标题时出现 "this.getService is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58031563/

相关文章:

Cordova 尝试从存储库获取插件而不是本地插件

javascript - NestJs 异步 httpService 调用

javascript - IE8 中的 __proto__ 使用 ko

javascript - 如何从 Node 服务器下载文件(仅使用 Node 模块,不使用express等)

combobox - OpenUI5 : Binding sap. m.ComboBox 到 sap.ui.table.Table 中的 JSONModel() 列

css - FIORI 自定义主题被部分用户的 library.css 覆盖

javascript - ODATA 请求已中止

javascript - jquery-validator 无法验证语义 UI 下拉列表

javascript - 从控件中检索 id 以在导航中使用

google-chrome - UI5 TileContainer 在 Chrome 中是空白的