"Modules that use an anonymous define() call must be loaded with a require() call"

"Modules that use an anonymous define() call must be loaded with a require() call"

walkthrough step 7: JSON Model 示例中,应用程序显然按照记录运行,但我在控制台中看到以下错误:

Error: Modules that use an anonymous define() call must be loaded with a require() call; they must not be executed via script tag or nested into other modules.

我能找到的此消息的唯一其他实例,在我未经训练的眼中,似乎完全是 different scenario

我已经尝试过 Firefox 和 Chromium,CDN 托管与本地托管,两个不同的 UI5 版本(1.77.0 和 1.79.0),都是缩小版和普通版,所以我想这确实是code itself.

会是什么?另外,我可以安全地忽略它吗?为什么?

TL;DR

  • index.html
  • 中删除 <script src="index.js"></script>
  • 如果源未定义模块,请将 sap.ui.define 替换为 sap.ui.require

更新

感谢您让我们意识到这个问题。 fix 将在下一个稳定版本中可用。 如果您访问 step 7 sample in the nightly build,您可以看到错误不再发生。文档也已相应修复。

原回答

目前,第 7 步加载 index.js 两次,这是错误的:

  1. 通过<script src="index.js"></script>
  2. 通过data-sap-ui-oninit="module:sap/ui/demo/walkthrough/index"
<script id="sap-ui-bootstrap"
  src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
  <strong>data-sap-ui-oninit="module:sap/ui/demo/walkthrough/index"</strong>
  ...
></script>
<strong><script src="index.js"></script></strong>

通过手动 <script> 标记进行的第一次加载是多余的,并且会导致有关“匿名 define() 调用”的错误,因为 index.js 正在尝试定义一个模块(即调用 sap.ui.define) 没有指定相应的模块名称,因此,anonymous。这种模块定义应该通过普通的 <script> 标记 neither 加载,例如在我们这里的例子中,模块中的 nor以嵌套方式定义。[1]

index.js 调用 sap.ui.define 也不干净,因为那里没有定义模块。相反,它应该调用 sap.ui<strong>.require</strong>.