【问题标题】:Loading amcharts libraries in SAPUI5 - component.js vs index.html在 SAPUI5 中加载 amcharts 库 - component.js vs index.html
【发布时间】:2019-04-17 22:52:23
【问题描述】:

我在 SAPUI5 中加载 amcharts 库时遇到问题。我要加载 3 个 amCharts 库,核心、图表和动画。我将它们放在我的项目结构中的一个名为 libs 的文件夹中...请注意,我想使用 component.js 文件而不是 index.html,因为我的应用程序将从 Fiori Launchpad 启动。

经过一些很好的建议,我现在将它们加载到 manifest.json 文件中,如下所示

        "resources": {
            "js": [
                {
                    "uri": "libs/core.js"
        },
                        {
                    "uri": "libs/charts.js"
        },
                        {
                    "uri": "libs/animated.js"
        }
        ],

            "css": [
                {
                    "uri": "css/style.css"
                }
            ]
        },

当我通过 index.html 文件测试应用程序时......它可以工作!

当我使用 component.js 文件进行测试时,我在控制台中收到以下错误

Failed to load resource: the server responded with a status of 404 ()
The issue is most likely caused by application znrw.amCharts. Please create a support incident and assign it to the support component of the respective application. - Failed to load UI5 component with properties: '{
    "asyncHints": {
        "waitFor": []
    },
    "name": "znrw.amCharts",
    "url": "../../../../../webapp",
    "id": "application-Test-url-component",
    "componentData": {
        "startupParameters": {}
    },
    "async": true
}'. Error likely caused by:
TypeError: Cannot read property '1' of null

error in launchpad

所以我的问题是,我是否必须在 component.js 文件中添加一些内容才能加载库?还是我需要在 manifest.json 文件中做更多的事情?

目前,组件文件如下所示:

    "sap/ui/core/UIComponent",
    "sap/ui/Device",
    "znrw/amCharts/model/models"
], function (UIComponent, Device, models) {
    "use strict";

    return UIComponent.extend("znrw.amCharts.Component", {

        metadata: {
            manifest: "json"
        },

        /**
         * The component is initialized by UI5 automatically during the startup of the app and calls the init method once.
         * @public
         * @override
         */
        init: function () {
            // call the base component's init function
            UIComponent.prototype.init.apply(this, arguments);

            // enable routing
            this.getRouter().initialize();

            // set the device model
            this.setModel(models.createDeviceModel(), "device");
        }
    });
});

【问题讨论】:

  • 在没有导入 amcharts 库的情况下是否可以工作?我不知道 amcharts,但如果 amCharts 全局定义或不支持 amd,你最好将它们加载到清单的 resources 部分。
  • 是的。你能告诉我如何在清单中加载它们吗?我不知道这是可能的吗?

标签: sapui5 amcharts


【解决方案1】:

在清单的sap.ui 部分,您会找到resources 的条目。通常它只有一个 CSS 文件,但您也可以定义外部 javascript 库。如果您要加载多个依赖项,请确保将它们按顺序排列

    "resources": {
        "js": [{
            "uri": "libs/mylib.js"
        }],
        "css": [{
            "uri": "css/style.css",
            "id": "style"
        }]
    }

如果一切顺利,您就可以使用window.amChart 或任何他们称为全局对象的对象

【讨论】:

  • 太棒了,我明天会试试这个并发布我的结果!非常感谢您的帮助:)
  • @NeilWard 是的,您可以添加屏幕截图,这会很有帮助
  • 嗨 Jorg,我已经上传了错误的屏幕截图,并且我还更新了问题的描述,因为我已经取得了进一步的成功。我认为您在清单中加载库的建议是一个很好的建议。现在,当我使用 index.html 测试我的应用程序时,它可以工作了!但是当我使用 component.js (foiri lauchpad) 测试它时它不起作用,这是我的最终目标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多