Javascript 组件获取错误。getView不是函数

Javascript 组件获取错误。getView不是函数,javascript,html,sapui5,Javascript,Html,Sapui5,我正在浏览sapui5演练教程,并已成功进入第9步,在该步骤中,我将教您如何在应用程序中使用Component.js文件 现在,在使用Component.js之前,应用程序中的一切都运行良好。但是,一旦我尝试使用组件,就会出现以下错误: Uncaught TypeError: this.getView is not a function 参考UIComponent.js第6行。尽管我的组件文件名为component.js。我还得到: GET http://localhost:58736/Inv

我正在浏览sapui5演练教程,并已成功进入第9步,在该步骤中,我将教您如何在应用程序中使用Component.js文件

现在,在使用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>
我的控制器

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);
      }
   });
});

在组件中,您不能调用此.getView(),因为没有getView(),api在

相反,您可以直接在组件本身上设置模型。换句话说,打电话就行了

this.setModel(oModel);

顺便说一句:在WalkTour中,它是以同样的方式完成的。

这个.getView()将不会在component.js中定义,因为视图尚未实例化


您只需使用这个.setModel(oModel)设置模型,就可以通过这个.setModel(oModel,“modelName”)设置模型名称

我在步行时也遇到了同样的问题

视图未在组件中实例化

你可以重写为

this.setModel("myModel);
而对于i18n

this.setModel(i18nModel,"i18n");
这个错误在漫游中就存在了,但现在看来已经修复了。在我学习的过程中,SAP UI5教程出现了一些错误,但现在它们已经修复了大部分错误

这也是从UI5开始的一个很好的源代码。几乎与Demokit相似

快乐编码

费宾·多米尼克

成功了!!!非常感谢:)我不知道SAP UI 5教程页面上有这个.getView()嗨,我有这个.setModel(oModel);在Component.js中,控制器就像第一篇文章一样,但仍然不工作。我在component.js中有getView()错误。我得到了按钮和输入(在我的view.js中定义),但由于错误,按下按钮时没有动作,也没有输入描述。如何替换控制器中的getView?
this.setModel("myModel);
this.setModel(i18nModel,"i18n");