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

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

据我了解,在很多情况下 sap.ui.requiresap.ui.define 是可以互换的。但是,有一个 convention:

  • Use sap.ui.define for controllers and all other JavaScript modules to define a global namespace. 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.

这个声明我读了好几遍,但不确定我是否完全理解。 例如,当我为特定的 UI5 视图编写一个 UI5 控制器时,我应该使用什么?当我写一个 BaseController.js 时,其余的控制器将基于它,那么我应该选择 sap.ui.define 还是 sap.ui.require

According对SAPUI5团队来说,sap.ui.define类似于ES6模块中静态的importexport语句,而sap.ui.require更像是动态 import() 语句。

基本上所有 UI5 模块定义(包括视图控制器的代码)都应该在顶层以 sap.ui.define 开头,而如果在代码中间我想使用 sap.ui.require导入一些其他 UI5 模块。

有关该主题的更多详细信息:Differences between sap.ui.require and sap.ui.define