首页 > 解决方案 > 未捕获的 TypeError:Fragment.load 不是函数

问题描述

下面的代码是从 UI5 Demo Kit 复制的,但是当我运行它时,控制台显示该函数Fragment.load不是函数的错误消息。请提出任何替代方案或突出显示问题(如果有)。

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "sap/m/MessageToast",
  "sap/ui/model/Filter",
  "sap/ui/model/FilterOperator",
  "sap/ui/model/json/JSONModel",
  "sap/m/MessageToast",
  "sap/ui/core/Fragment"
], function(Controller, MessageToast, Filter, FilterOperator, JSONModel, Fragment) {
  "use strict";

  return Controller.extend("Workspace.controller.HelloPanel", {
    onInit: function() {
      var plant = {
        pid: "",
        ptype: "",
        pdesc: "",
        psite: "",
        pstatus: "",
        passigned: "",
        pattach: ""
      };
      var oModel1 = new JSONModel(plant);
      this.getView().setModel(oModel1, "SUP");
    },

    onOpenDialog: function() {
      var oView = this.getView();
      if (!this.byId("helloDialog")) {
        Fragment.load({
          id: oView.getId(),
          name: "Workspace.view.HelloDialog",
          controller: this
        }).then(function(oDialog) {
          // connect dialog to the root view of this component (models, lifecycle)
          oView.addDependent(oDialog);
          oDialog.open();
        });
      } else {
        this.byId("helloDialog").open();
      }
    },

    onCloseDialog: function() {
      this.byId("helloDialog").close();
    },

  });
});

标签: sapui5amd

解决方案


原因1:依赖项与所需参数不匹配

调用sap.ui.defineor.require时,请确保依赖项和回调参数以相同的顺序列出:

sap.ui.define([ // list of dependencies
  "sap/ui/core/mvc/Controller", // 1st
  "sap/m/AnotherModule", // 2nd
  // etc...
], function(/*required modules: */Controller/*1st*/, AnotherModule/*2nd, etc...*/) {
  // ...
});

例如,在上面的问题中,我们可以看到"sap/m/MessageToast"意外需要两次,导致与回调参数列表不匹配。"sap/m/MessageToast"从依赖列表中删除第二个。否则,您会尝试.load()从 MessageToast 调用,因此会出现错误。


原因2:该方法是在后来的版本中引入的

如果您遇到相同的错误,尽管依赖顺序正确,请记住 UI5Fragment.load1.58中首先引入。

要查看应用程序实际运行的是哪个 UI5 版本,请按Ctrl+ Shift+ Left Alt+ P


推荐阅读