_oDialog.destroy() 后出现“setInitialFocusId”错误

san*_*oxj 3 javascript sapui5

在调用后尝试第二次打开对话框片段时,出现以下错误this._oDialog.destroy()

未捕获的类型错误:无法读取 null 的属性“setInitialFocusId”

我的问题就像这里提到的问题:How to clear dialog/xmlfragment content after close? 但是,解决方案显然只是“不要使用属性 setInitialFocus”,我没有在代码中的任何地方使用它。

控制器

openDialog: function() {
  if (!this._oDialog) {
    this._oDialog = sap.ui.xmlfragment("myFragmentPath", this);
    this.getView().addDependent(this._oDialog);
  }
  this._oDialog.open();
},

onExit: function () {
  if (this._oDialog) {
    this._oDialog.destroy();
  }
},

afterClose: function () {
  if (this._oDialog) {
    this._oDialog.destroy();
  }
},

handleClose: function (oEvent) {
  this._oDialog.close();
}
Run Code Online (Sandbox Code Playgroud)

对话片段

openDialog: function() {
  if (!this._oDialog) {
    this._oDialog = sap.ui.xmlfragment("myFragmentPath", this);
    this.getView().addDependent(this._oDialog);
  }
  this._oDialog.open();
},

onExit: function () {
  if (this._oDialog) {
    this._oDialog.destroy();
  }
},

afterClose: function () {
  if (this._oDialog) {
    this._oDialog.destroy();
  }
},

handleClose: function (oEvent) {
  this._oDialog.close();
}
Run Code Online (Sandbox Code Playgroud)

主 XML 视图

<Dialog xmlns="sap.m" afterClose=".afterClose">
  <!-- ... -->
</Dialog>
Run Code Online (Sandbox Code Playgroud)

附加信息:

  • this._oDialog.open();被调用时,错误信息出现在控制器行中。
  • 我正在使用 sap 库版本 1.60.1。

Bog*_*ann 6

if (this._oDialog) {
  this._oDialog.destroy();
  this._oDialog = null; // make it falsy so that it can be created next time
}
Run Code Online (Sandbox Code Playgroud)

关闭后,对话框在您的代码中被销毁。然而,this._oDialog仍然存在。

由于this._oDialog不是虚假值而只是一个销毁的对话框实例,因此openDialog()第二次没有创建新的对话框。因此,您试图打开一个被破坏的对话框。

当被破坏的对话框中,其内部oPopup设置为null,这也解释了错误信息。


?? 笔记

  1. 关闭后通常不需要销毁对话框。当视图被销毁时,对话框将自动销毁,因为片段依赖于视图。如果目的是重置数据值,请尝试解除绑定属性,而不是每次都销毁并重新创建整个片段,这代价很高。

  2. 从 UI5 1.56 开始,sap.ui.xmlfragment不推荐使用工厂函数因为它通过同步 XHR(阻塞主线程)获取片段。使用新的异步 API 之一

  3. 一个更简单的选择是在您的视图定义中以声明方式将片段添加<core:Fragment fragmentName="..." type="XML" /><dependents>某个控件的聚合中。就像在这个示例中一样