【问题标题】:Uncaught TypeError: Cannot read property 'initialize' of undefined未捕获的类型错误:无法读取未定义的属性“初始化”
【发布时间】:2019-12-30 12:41:57
【问题描述】:

目前我正在做一个升级项目;当我从 Component.js 实例化自定义 Router 类 时,我遇到了下面的 Exception;应用程序特定的配置在单独的 ma​​nifest.json 文件中配置。问题是我无法从 Component.js 读取 manifest.json 配置。

我从 Scratch 创建了 SAP Ui5 应用程序,我通过 SAP 的最佳方法实现了这些应用程序,它们都运行良好,我什至交叉检查了一个工作 Ui5 应用程序的清单文件,一切都很完美,但我仍然无法发现问题,我得到以下异常。

异常/错误:

UIComponent.js:6 Uncaught TypeError: Cannot read property 'initialize' of undefined
    at f.init (Component.js:21)
    at f.constructor (sap-ui-core.js:135)
    at f.constructor (sap-ui-core.js:141)
    at f.constructor (UIComponent.js:6)
    at new f (sap-ui-core.js:135)
    at Object.sap.ui.component (sap-ui-core.js:141)
    at F.a.onBeforeRendering (ComponentContainer.js:6)
    at F.a._callEventHandles (sap-ui-core.js:147)
    at F.a._handleEvent (sap-ui-core.js:147)
    at t (sap-ui-core.js:147)

Component.js [请注意:下面我只分享了init方法]

sap.ui.define([
    "sap/ui/core/UIComponent",
    "xxxx/app/xxxxx/MyRouter"
], function (UIComponent , MyRouter ) {

    return  UIComponent.extend("xxxx.app.xxxxxx.Component", {

        metadata: {
            manifest: "json"
        },

    init : function() {
        debugger; // IMDK901184 
        UIComponent.prototype.init.apply(this, arguments);

        // I am getting the exception once the below line is executed.
        this.getRouter().initialize();  

    }
}
    });
});

manifest.json

{
    "_version": "1.12.0",
    "sap.app": {
        "id": "xxxxx.app.xxxxx",
        "type": "application",
        "i18n": "i18n/messageBundle.properties",
        "title": "xxxx",
        "description": "xxxx",
        "applicationVersion": {
            "version": "1.0.0"
        },
        "dataSources": {
            "mainService": {
                "uri": "xxxx.app.xxxxxx.AppScripts.config.baseURL",
                "type": "OData",
                "settings": {
                    "odataVersion": "2.0"
                }
            }
        }
    },
        "sap.ui": {
            "technology": "UI5",
            "deviceTypes": {
                "desktop": true,
                "tablet": true,
                "phone": true
            },
            "supportedThemes": [
                "sap_bluecrystal"
                ]
        },
        "sap.ui5": {

            "rootView": {
                  "viewName": "xxxxx.app.xxxxxx.POSView.App",
                  "type": "XML",
                  "async": true,
                  "id": "idAppControl"
              } ,
             "dependencies": {
                "minUI5Version": "1.30",
                "libs": {
                    "sap.m": {} ,
                    "sap.ui.core": {},
                    "sap.ui.layout": {}
                }
            },
            "models": {
                "i18n": {
                    "type": "sap.ui.model.resource.ResourceModel",
                    "settings": {
                        "bundleName": "xxxx.app.xxxxxx.i18n.messageBundle"
                    }
                },
                "oMainModel": {
                    "dataSource": "mainService",
                    "settings": {
                        "useBatch": false
                    }
                }
            } ,

            "routing" : {
                "config" : {
                "routerClass" : "xxxxxx.app.xxxxxx.MyRouter", 
                "viewType" : "XML",
                "viewPath" : "xxxxxx.app.xxxxxx.POSView",
                "controlId": "idAppControl",
                "targetAggregation" : "detailPages",
                "clearTarget" : "false"
            } ,

            "routes" : [
                {

                ]
        }
    }

}   

【问题讨论】:

  • 查看this.getRouter()的回复
  • this.getRouter() 未定义

标签: javascript exception sapui5 instantiation


【解决方案1】:

我猜因为当前安装的 SAP Ui5 版本是 1.26,所以这里不支持新类型的声明 Manifest.json。

重构回component.js中的元数据部分

【讨论】:

  • 尽快将 UI5 库升级到 latest appropriate version 应该是您组织中的最高优先事项之一。 1.26 是一个古老的版本,存在多个安全问题并错过了性能优化机会。
【解决方案2】:

我认为您需要实现 getRouter() 方法,例如:

init : function() { 
..

..
}    
getRouter : function () {
    return sap.ui.core.UIComponent.getRouterFor(this);
}

【讨论】:

  • 感谢您的回复!但是我已经在清单文件的 routing->Config 部分给出了路由器类的详细信息。因此,一旦组件初始化,清单就会被初始化,因此我可以从 Component.xml 访问配置。但我做不到!
猜你喜欢
  • 2015-11-01
  • 2021-12-22
  • 2015-01-06
  • 2017-07-26
  • 2019-02-26
  • 2021-12-25
  • 1970-01-01
相关资源
最近更新 更多