2017-05-05 84 views
-1
<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-language="en" 
      data-sap-ui-libs="sap.ui.commons,sap.ui.table" 
      data-sap-ui-compatVersion="edge" 
      data-sap-ui-preload="async" 
      data-sap-ui-theme="sap_bluecrystal"> 
    </script> 
    <script> 
     sap.ui.localResources("my_project"); 
     sap.ui.localResources("util"); 
     sap.ui.localResources("i18n"); 


      var view = sap.ui.view({type: sap.ui.core.mvc, 
       id: "xsodata", 
       viewName: "my_project.view.xsodatatest" 

      }); 
      view.placeAt("content"); 

    </script> 
</head> 
<body class="sapUiBody" role="application"> 
<div id="content"> 
</div> 
</body> 
</html> 

這會產生一個錯誤:SAPUI5:初始化JS視圖 - sap.ui.view不是一個函數

Uncaught TypeError: sap.ui.view is not a function

然而,加載XML意見工作完全正常。我看了很多教程/代碼,他們都使用與我在這裏相同的語法,所以我真的不明白爲什麼這不起作用?

任何人都可以點亮這個?

感謝

回答

0

你在你的代碼的幾個問題:

  • sap.ui.commons已過時,所以不要使用它。最有可能你會加載sap.m,而不是...

  • 你已經設置預加載異步,這是很好的。然而,那麼你應該明白這意味着什麼。在代碼中使用attach init(詳見下文)。

  • 視圖類型必須是sap.ui.core.mvc.ViewType.JS而不是sap.ui.core.mvc:

  • 你也可以使用 「sap.ui.jsview(......)」 直接(類似於sap.ui. xmlView中,()在我下面的代碼)

這會幫助你:

var oView = sap.ui.view({ 
    viewName: "my_project.view.xsodatatest", 
    type: sap.ui.core.mvc.ViewType.JS 
}); 
oView.placeAt("content"); 

或者,也許這一個位置:

var oView = sap.ui.jsview("my_project.view.xsodatatest"); 
oView.placeAt("content"); 

反正這裏是xmlView中(也可通過jsbin)另一個簡單的例子:

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
     <meta charset="utf-8"> 
 
     <title>SAPUI5 single file template | nabisoft</title> 
 
     
 
     <script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js" 
 
      id="sap-ui-bootstrap" 
 
      data-sap-ui-theme="sap_belize" 
 
      data-sap-ui-libs="sap.m" 
 
      data-sap-ui-bindingSyntax="complex" 
 
      data-sap-ui-compatVersion="edge" 
 
      data-sap-ui-preload="async"></script> 
 
      <!-- use "sync" or change the code below if you have issues --> 
 
    
 
     <!-- XMLView --> 
 
     <script id="myXmlView" type="ui5/xmlview"> 
 
      <mvc:View 
 
       xmlns="sap.m" 
 
       xmlns:core="sap.ui.core" 
 
       xmlns:mvc="sap.ui.core.mvc"> 
 
    
 
       <Text text="Hello Nabi" /> 
 
    
 
      </mvc:View> 
 
     </script> 
 
    
 
     <script> 
 
      sap.ui.getCore().attachInit(function() { 
 
       "use strict"; 
 
    
 
       sap.ui.xmlview({ 
 
        viewContent : jQuery("#myXmlView").html() 
 
       }).placeAt("content"); 
 
    
 
      }); 
 
     </script> 
 
    
 
    </head> 
 
    
 
    <body class="sapUiBody"> 
 
     <div id="content"></div> 
 
    </body> 
 
</html>

+0

嗨。 我已經嘗試過使用attachinit,這就是我現在使用我的xml視圖的方式。但是,當我嘗試使用js視圖時,我得到:錯誤:錯誤參數('[object Object]','undefined')!要麼調用sap.ui.jsview([sId,] sViewName)來實例化一個View或sap.ui.jsview(sViewName,oViewImpl)來定義一個View類型。 不知道我在做什麼錯在這裏... – Louis

+0

因此,這是一個不同的錯誤了,因爲上面你提到的「遺漏的類型錯誤:sap.ui.view不是一個函數」。隨着新的錯誤消息,它似乎你是以錯誤的方式調用sap.ui.jsview(...)。然而,沒有人知道你的「改變」的調用/代碼是什麼樣子的。 – Nabi

+0

我實際上找到了解決方案。 Appereantly你必須做這樣的: sap.ui.jsview( '身份識別碼', 「mypath中」)。所以沒有{},爲什麼它不起作用。既然你確實需要初始化,我會接受你的答案,謝謝你的幫助。 – Louis