TypeError:无法读取属性'<类名的一部分>'未定义 [英] TypeError: Cannot read property '<part of class name>' of undefined

查看:88
本文介绍了TypeError:无法读取属性'<类名的一部分>'未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在SAPUI5中添加 InfoLabel

  new sap.tnt.InfoLabel({/*...*/); 

但是此错误在重复:

TypeError:无法读取未定义的属性'InfoLabel'

解决方案

  1. 请确保在清单中的/sap.ui5/dependencies/libs 声明目标模块所属的所有依赖库.json .例如,对于 sap.tnt.InfoLabel :

     <代码>"sap.ui5":{依赖关系":{"libs":{"sap.ui.core":{},"sap.m":{} ,"sap.tnt":{} }}, 

    注意: manifest.json 也称为

    要查看您的应用运行的UI5版本,请按 Ctrl + Shift + 左Alt + P .

    I have tried to add the InfoLabel in SAPUI5

    new sap.tnt.InfoLabel({/*...*/);
    

    But this error is repeating:

    TypeError: Cannot read property 'InfoLabel' of undefined

    解决方案

    1. Make sure to declare all dependent libraries, to which the target modules belong, in /sap.ui5/dependencies/libs within manifest.json. For sap.tnt.InfoLabel for example:

      "sap.ui5": {
        "dependencies": {
          "libs": {
            "sap.ui.core": {},
            "sap.m": {},
            "sap.tnt": {}
          }
        },

      Note: manifest.json is also called Descriptor for Applications, Components, and Libraries or simply app descriptor.

      If there is no manifest.json, add the dependent libraries to data-sap-ui-libs in index.html:

      <script id="sap-ui-bootstrap" src="..." data-sap-ui-libs="sap.ui.core,sap.m,sap.tnt">

    2. Modules should be accessed only after their dependent libs are preloaded. This can be ensured using various APIs depending on the use case:

      • Declaratively using sap/ui/core/ComponentSupport.js (Recommended):

        <script id="sap-ui-bootstrap" src="..."
          data-sap-ui-async="true"
          data-sap-ui-resourceroots='{"my.demo": "./"}'
          data-sap-ui-oninit="module:sap/ui/core/ComponentSupport"
          ...
        ></script>
        <body class="sapUiBody" id="content">
          <div style="height: 100%;"
            data-sap-ui-component
            data-id="rootComponentContainer"
            data-name="my.demo"
            data-height="100%"
            data-settings='{ "id": "rootComponent" }'
          ></div>
        </body>

        Doc: Declarative API for Initial Components, Sample: https://embed.plnkr.co/16J1TFICxbqETCzaxuZ0

      • Using an inline-script (Only if there is no manifest.json; only for small demos):

        <script id="sap-ui-bootstrap" src="..."
          data-sap-ui-libs="sap.ui.core, sap.m, sap.tnt"
          data-sap-ui-async="true"
          ...
        ></script>
        <script>
          sap.ui.getCore().attachInit(function() {
            // your code...
          });
        </script>
        <body class="sapUiBody" id="content"></body>

    Additionally, avoid referencing controls via global names (e.g. sap.tnt.…)! Instead, require the modules accordingly:

    sap.ui.define([ // Dependency list; requiring the modules:
      "sap/ui/core/mvc/Controller",
      "sap/tnt/InfoLabel",
    ], function(Controller, InfoLabel) {
      "use strict";
    
      return Controller.extend("myController", {
        someMethod: function(/*...*/) {
          const myInfoLabel = new InfoLabel({/*...*/}); // without a global name
          // ...
        },
      });
        
    });
    

    From the doc:

    [...] your application modules have to facilitate the concept for defining and handling of modules in UI5 that is aligned with the asynchronous module definition (AMD) standard.

    [...] All necessary module dependencies [...] need to be handled by sap.ui.require or sap.ui.define.

    See also the topic Modules and Dependencies.


    It's also important to always check if the target module is available in the UI5 version at all. For example, the sap.tnt.InfoLabel was introduced in 1.54. You might get a 404-error if you're trying to require a module that doesn't exist yet.

    In order to see which UI5 version your app is running with, press Ctrl+Shift+Left Alt+P.

    这篇关于TypeError:无法读取属性'&lt;类名的一部分&gt;'未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆