在 UI5 中实现 IAsyncContentCreation 接口

Implementing the IAsyncContentCreation interface in UI5

确保sap.ui.core.UIComponent to be created fully asynchronously, we need to implement the IAsyncContentCreation接口:

metadata: {
    interfaces: [
        "sap.ui.core.IAsyncContentCreation"
    ],
    manifest: "json"
}

完全可以。
探索 SAP 应用程序示例,我发现了以下代码片段,其中硬编码且因此容易出错的接口名称被替换为代码,可以自动检查:

sap.ui.define([
    "sap/ui/core/library",
    "sap/ui/core/UIComponent",
    …
], function(library, UIComponent, models, History) {
    "use strict";

    return UIComponent.extend("sap.ui.demo.toolpageapp.Component", {

        metadata: {
            interfaces: [
                library.IAsyncContentCreation
            ],
            manifest: "json"
        }
        …

    }
}

我试图在 DevTools 中检查 library 的内容,但我得到了 ReferenceError 异常而不是 library 的值:

Uncaught ReferenceError: library is not defined
    at eval (eval at init (Component.js:24), <anonymous>:1:1)
    at f.init (Component.js:24)
    at sap-ui-core.js:315
    at f.constructor (sap-ui-core.js:315)
    at f.constructor (sap-ui-core.js:585)
    at f.constructor (library-preload.js:1154)
    at f [as constructor] (sap-ui-core.js:547)
    at new o1 (sap-ui-core.js:632)
    at H (sap-ui-core.js:628)
    at sap-ui-core.js:628

如何确保 library.IAsyncContentCreation 真正正确定义并且 returns "sap.ui.core.IAsyncContentCreation" 符合预期?

I've tried to check the content of library in DevTools, but I get a ReferenceError exception.

很可能,您遇到了 V8 的调试器限制,其中未使用的闭包变量(此处:library)在调试器会话期间未被 V8 评估,因此无法从 devtool 控制台访问.参见 and also the V8 issues #3491 and #2710

以上限制仅适用于 V8 的调试器。当代码在运行时执行时,library.IAsyncContentCreation 将按预期解析为字符串 "sap.ui.core.IAsyncContentCreation",因此您不会得到 ReferenceError.

Exploring the SAP apps samples, [...] interface name is replaced with code.

尚不清楚为什么示例作者首先决定需要核心库。那些 interfaces in UI5 Objects await string literals. 和 UI5 界面是简单的标记界面(又名标记界面)。他们没有在代码中指定任何行为。添加接口名称(字符串文字)只是告诉 class 实现该接口的行为方式。所以添加字符串文字就足够了。

interfaces: [
  "sap.ui.core.IAsyncContentCreation"
],