简体   繁体   English

我应该使用 `sap.ui.require` 或 `sap.ui.define` 中的哪一个?

[英]Which of `sap.ui.require` or `sap.ui.define` should I use?

As far as I understand, in many cases sap.ui.require and sap.ui.define are interchangeable.据我了解,在很多情况下sap.ui.requiresap.ui.define是可以互换的。 However, there is a convention :但是,有一个约定

  • Use sap.ui.define for controllers and all other JavaScript modules to define a global namespace.对控制器和所有其他 JavaScript 模块使用sap.ui.define来定义全局命名空间。 With the namespace, the object can be addressed throughout the application.使用命名空间,可以在整个应用程序中寻址对象。

  • Use sap.ui.require for asynchronously loading dependencies but without declaring a namespace, for example code that just needs to be executed, but does not need to be called from other code.使用sap.ui.require异步加载依赖项但不声明命名空间,例如只需要执行但不需要从其他代码调用的代码。

I read this statement several times, but I'm not sure I got it completely.我多次阅读此声明,但我不确定我是否完全理解。 For instance, when I write an UI5-controller for the specific UI5-view, then what should I use?例如,当我为特定的 UI5-view 编写一个 UI5-controller 时,我应该使用什么? And when I write a BaseController.js , which the rest of the controllers will be based on, then should I chose sap.ui.define or sap.ui.require ?当我编写一个BaseController.js ,其余的控制器将基于它,那么我应该选择sap.ui.define还是sap.ui.require

According to the SAPUI5 team, sap.ui.define is similar to the static import and export statements in ES6 modules, while sap.ui.require is more like the dynamic import() statement. 根据SAPUI5 团队的说法, sap.ui.define类似于 ES6 模块中的静态importexport语句,而sap.ui.require更类似于动态import()语句。

Basically all UI5 module definitions (incl. view controllers' code) should start with sap.ui.define on the top level, while sap.ui.require should be used if in the middle of the code I want to import some other UI5 modules.基本上所有UI5模块定义(包括视图控制器的代码)应该开始sap.ui.define在顶层,而sap.ui.require应使用如果代码中间我想进口一些其他UI5模块.

For more details on the subject: Differences between sap.ui.require and sap.ui.define .有关该主题的更多详细信息: sap.ui.requiresap.ui.define之间的差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM