Uncaught TypeError: Fragment.load is not a function
Uncaught TypeError: Fragment.load is not a function
下面的代码是从 UI5 演示套件复制的,但是当我 运行 它时,控制台显示错误消息,指出函数 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();
},
});
});
原因 1:依赖项与所需参数不匹配
调用 sap.ui.define
或 .require
时,确保 dependencies 和回调 parameters 已列出在相同的顺序中:
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"
不小心被require了两次,导致和回调参数列表不匹配。从依赖项列表中删除第二个 "sap/m/MessageToast"
。否则,您正试图从 MessageToast 调用 .load()
,因此会出现错误。
原因2:该方法是后版本引入的
如果您遇到相同的错误,尽管依赖顺序正确,请记住 UI5 首先在 1.58.[=18= 中引入了 Fragment.load
]
要查看应用实际是哪个 UI5 版本 运行,请按 Ctrl+Shift+左 Alt+P.
下面的代码是从 UI5 演示套件复制的,但是当我 运行 它时,控制台显示错误消息,指出函数 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();
},
});
});
原因 1:依赖项与所需参数不匹配
调用 sap.ui.define
或 .require
时,确保 dependencies 和回调 parameters 已列出在相同的顺序中:
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"
不小心被require了两次,导致和回调参数列表不匹配。从依赖项列表中删除第二个 "sap/m/MessageToast"
。否则,您正试图从 MessageToast 调用 .load()
,因此会出现错误。
原因2:该方法是后版本引入的
如果您遇到相同的错误,尽管依赖顺序正确,请记住 UI5 首先在 1.58.[=18= 中引入了 Fragment.load
]
要查看应用实际是哪个 UI5 版本 运行,请按 Ctrl+Shift+左 Alt+P.