2015-09-26 158 views
1

我正在瀏覽sap ui5演練教程,並已設法進入步驟9,它將教您如何在應用程序中使用Component.js文件。SAP UI5 - 組件不工作(this.getView不是函數)

現在,在使用Component.js之前,應用程序中的所有內容都正常工作。但是,一旦我嘗試使用的組件我得到這個錯誤:

Uncaught TypeError: this.getView is not a function 

參考UIComponent.js線6.即使我的組件文件就被稱爲Component.js。我也得到:

GET http://localhost:58736/InvoicesApp/invoicesapp/Component-preload.js 404 (Not Found) 

但我不知道他們是相關

這裏是我的index.html

<!DOCTYPE HTML> 
<html> 
    <head> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/> 

     <script src="resources/sap-ui-core.js" 
       id="sap-ui-bootstrap" 
       data-sap-ui-libs="sap.m" 
       data-sap-ui-theme="sap_bluecrystal" 
       data-sap-ui-bindingSyntax="complex" 
       data-sap-ui-compatVersion="edge" 
       data-sap-ui-preload="async" 
       > 
     </script> 
     <!-- only load the mobile lib "sap.m" and the "sap_bluecrystal" theme --> 

     <script> 
       sap.ui.localResources("invoicesapp"); 
       sap.ui.getCore().attachInit(function() { 
        new sap.ui.core.ComponentContainer({ 
          name : "invoicesapp" 
         }).placeAt("content"); 

       }); 
     </script> 

    </head> 
    <body class="sapUiBody" id="content"/> 
</html> 

我Component.js

sap.ui.define([ 
    "sap/ui/core/UIComponent", 
    "sap/ui/model/json/JSONModel", 
    "sap/ui/model/resource/ResourceModel" 
], function (UIComponent, JSONModel, ResourceModel) { 
    "use strict"; 
    return UIComponent.extend("invoicesapp.Component", { 
     metadata: { 
      rootView:"invoicesapp.view.App" 
     }, 

     init : function() { 
     // call the init function of the parent 
     UIComponent.prototype.init.apply(this, arguments); 

     // set data model on view 
     var oData = { 
      recipient : { 
       name : "World" 
      } 
     }; 
     var oModel = new JSONModel(oData); 
     this.getView().setModel(oModel); 
    // set i18n model on view 
     var i18nModel = new ResourceModel({ 
      bundleName: "invoicesapp.i18n.i18n" 
     }); 
     this.getView().setModel(i18nModel, "i18n"); 
    } 
    }); 
}); 

我控制器

sap.ui.define([ 
    "sap/ui/core/mvc/Controller", 
    "sap/m/MessageToast", 
    "sap/ui/model/json/JSONModel", 
    "sap/ui/model/resource/ResourceModel" 
], function (Controller, MessageToast, JSONModel, ResourceModel) { 
    "use strict"; 
    return Controller.extend("invoicesapp.controller.App", { 

     onShowHello : function() { 

     // read msg from i18n model 
     var oBundle = this.getView().getModel("i18n").getResourceBundle(); 
     var sRecipient = this.getView().getModel().getProperty("/recipient/name"); 
     var sMsg = oBundle.getText("helloMsg", [sRecipient]); 
     // show message 
     MessageToast.show(sMsg); 
     } 
    }); 
}); 

回答

0

在組件你不能叫this.getView()因爲沒有getView(),在https://openui5beta.hana.ondemand.com/#docs/api/symbols/sap.ui.core.Component.html

API文檔而是直接組件本身設置的模式。換句話說,只需撥打

this.setModel(oModel); 

this.setModel(i18nModel, "i18n"); 

順便說一句:在walktrough它做了同樣的方式。

+0

它的工作!非常感謝:) 我不知道SAP UI 5的教程頁面有this.getView() – Adnan

+0

嗨,我有this.setModel(oModel);在Component.js和控制器像第一篇文章,但仍然沒有工作。我在component.js中有getView()錯誤。我得到按鈕和輸入(在我的view.js中定義),但沒有按下操作,也沒有由於錯誤導致的輸入描述。我如何更換控制器中的getView? – lorenag83

0

this.getView()將不會在component.js中定義,因爲視圖尚未實例化。您可以簡單地使用this.setModel(oModel)設置模型,您可以通過this.setModel(oModel,「modelName」)設置名稱模型。

0

我也有同樣的問題,而通過漫步。

視圖未在組件中實例化。

可以改寫爲

this.setModel("myModel); 

而對於國際化

this.setModel(i18nModel,"i18n"); 

這個錯誤在那裏的穿行,但現在它的固定看來。在我學習的時候,SAP UI5教程幾乎沒有錯誤,但現在他們已經修復了大部分錯誤。

這也是一個很好的從UI5開始的資源。幾乎類似於Demokit。

UI Development Toolkit for HTML5 (SAPUI5)

編碼快樂

Febin多米尼克