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

萝らか妹 提交于 2020-12-10 08:35:31

问题


In the walkthrough step 7: JSON Model example, the app apparently works as documented but I see the following error in the console:

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.

The only other instance of this message that I could find seems, to my untrained eye, to deal with a wholly different scenario.

I've tried both Firefox and Chromium, CDN hosting vs local hosting, two different UI5 versions (1.77.0 and 1.79.0), both minified and plain, so I'd suppose this is really something in the code itself.

What could it be? Also, is it something I can safely ignore and why?


回答1:


TL;DR

  • Remove <script src="index.js"></script> from index.html
  • Replace sap.ui.define with sap.ui.require if the source is not defining a module.

Update

Thanks for making us aware of the issue. The fix will be available in the next stable release. If you visit the step 7 sample in the nightly build, you can see that the error no longer occurs. Also the documentation is fixed accordingly.

Original answer

Currently, the step 7 loads index.js twice which is wrong:

  1. Via <script src="index.js"></script>
  2. Via 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"
  data-sap-ui-oninit="module:sap/ui/demo/walkthrough/index"
  ...
></script>
<script src="index.js"></script>

The 1st load via the manual <script> tag is superfluous and causes the error about the "anonymous define() call" since index.js is trying to define a module (i.e. calling sap.ui.define) without specifying a corresponding module name, hence, anonymous. This kind of module definition should be loaded neither via a plain <script> tag, such as in our case here, nor within a module definition in a nested way.[1]

index.js calling sap.ui.define isn't clean either since no module is being defined there. Instead, it should call sap.ui.require.



来源:https://stackoverflow.com/questions/62581851/modules-that-use-an-anonymous-define-call-must-be-loaded-with-a-require-cal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!