在 UI5 中定义模块
Defining modules in UI5
我试图将我的代码分成模块。当我定义我的第一个模块时,我扩展了 sap.ui.base.Object 并且它起作用了。我的问题是:在定义我自己的模块时是否必须扩展 sap.ui.base.Object?根据 API documentation 我尝试了以下示例:
sap.ui.define([], function() {
// create a new class
var SomeClass = function();
// add methods to its prototype
SomeClass.prototype.foo = function() {
return "Foo";
}
// return the class as module value
return SomeClass;
});
我在 Component.js 中需要这个模块作为依赖项,如下所示:
sap.ui.define([
"path/to/SomeClass"
], function (SomeClass) {
"use strict";
//var test = new SomeClass();
我总是收到语法错误:
failed to load '[...]/Component.js' from ./Component.js: Error: failed to load '[...]/module/SomeClass.js' from ./module/Service.js: SyntaxError: Unexpected token ;
有人知道为什么会这样吗?谢谢!
我们在模块中对代码进行分组,例如:
jQuery.sap.declare("our.namespace.iscool.util.Navigation");
our.namespace.iscool.util.Navigation = {
to: function (pageId, context) {
// code here
}
// etc.
}
并在控制器中像这样调用模块的功能
jQuery.sap.require("our.namespace.iscool.util.Navigation");
sap.ui.controller("our.namespace.iscool.Detail", {
// somewhere in this file comes this
handleNavButtonPress: function (evt) {
our.namespace.iscool.util.Navigation.navBackToMaster(
evt.getSource().getBindingContext()
);
},
}
愚蠢的错误 - 文档中缺少大括号。
var someclass = function() {} ;
我试图将我的代码分成模块。当我定义我的第一个模块时,我扩展了 sap.ui.base.Object 并且它起作用了。我的问题是:在定义我自己的模块时是否必须扩展 sap.ui.base.Object?根据 API documentation 我尝试了以下示例:
sap.ui.define([], function() {
// create a new class
var SomeClass = function();
// add methods to its prototype
SomeClass.prototype.foo = function() {
return "Foo";
}
// return the class as module value
return SomeClass;
});
我在 Component.js 中需要这个模块作为依赖项,如下所示:
sap.ui.define([
"path/to/SomeClass"
], function (SomeClass) {
"use strict";
//var test = new SomeClass();
我总是收到语法错误:
failed to load '[...]/Component.js' from ./Component.js: Error: failed to load '[...]/module/SomeClass.js' from ./module/Service.js: SyntaxError: Unexpected token ;
有人知道为什么会这样吗?谢谢!
我们在模块中对代码进行分组,例如:
jQuery.sap.declare("our.namespace.iscool.util.Navigation");
our.namespace.iscool.util.Navigation = {
to: function (pageId, context) {
// code here
}
// etc.
}
并在控制器中像这样调用模块的功能
jQuery.sap.require("our.namespace.iscool.util.Navigation");
sap.ui.controller("our.namespace.iscool.Detail", {
// somewhere in this file comes this
handleNavButtonPress: function (evt) {
our.namespace.iscool.util.Navigation.navBackToMaster(
evt.getSource().getBindingContext()
);
},
}
愚蠢的错误 - 文档中缺少大括号。 var someclass = function() {} ;