最近有閒跟着官方的Get Started教程學習了UI5,記錄一下自己學習中遇到的幾個問題。
本文鏈接:https://www.cnblogs.com/hhelibeb/p/17835722.html
1,文檔和實際代碼的一致性
注意文檔可能不是最新的,和實際示例代碼有出入,比如本文寫作時,Data Binding Tutorial裏面的Step 1: No Data Binding
教程裏寫的代碼是,
sap.ui.require([
"sap/m/Text"
], function (Text) {
"use strict";
// Attach an anonymous function to the SAPUI5 'init' event
sap.ui.getCore().attachInit(function () {
// Create a text UI element that displays a hardcoded text string
new Text({text: "Hi, my name is Harry Hawk"}).placeAt("content");
});
});
示例的實際代碼卻是,
sap.ui.require([
"sap/ui/core/Core",
"sap/m/Text"
], function (
Core,
Text
) {
"use strict";
// Chain an anonymous function to the SAPUI5 'ready' Promise
Core.ready().then(function () {
// Create a text UI element that displays a hardcoded text string
new Text({text: "Hi, my name is Harry Hawk"}).placeAt("content");
});
});
這是因爲在新版UI5中,attachInit方法已經Deprecated。
通常這樣的不一致沒有太大影響,但某些不一致也有可能會導致程序運行失敗,使用時需要注意。
截止目前,我已經向文檔提出2個PR用來修復這類不一致導致的程序運行失敗問題。
2,例子中的resources/sap-ui-core.js如何引用?
sap-ui-core.js是UI5的核心庫,大部分教程的index.html都會有類似代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>UI5 Walkthrough</title>
<script
id="sap-ui-bootstrap"
src="resources/sap-ui-core.js"
data-sap-ui-theme="sap_horizon"
data-sap-ui-libs="sap.m"
data-sap-ui-compatVersion="edge"
data-sap-ui-async="true"
data-sap-ui-onInit="module:ui5/walkthrough/index"
data-sap-ui-resourceroots='{
"ui5.walkthrough": "./"
}'>
</script>
</head>
<body>
<div>Hello World</div>
</body>
</html>
其中src="resources/sap-ui-core.js"用來引用sap-ui-core.js,對於本地的項目,我們可以替換鏈接爲:
src="https://ui5.sap.com/resources/sap-ui-core.js"
或者安裝SAP Fiori Tools代理,並且通過ui5.yaml配置來爲/resource路徑設置代理,這樣就不需要修改index.html中的src了。以下是部分配置代碼參考,
server:
customMiddleware:
- name: fiori-tools-proxy
afterMiddleware: compression
configuration:
ignoreCertError: false
ui5:
path:
- /resources
- /test-resources
url: https://ui5.sap.com
3,data-sap-ui-resourceroots
注意需要設置前文index.html中的data-sap-ui-resourceroots,這個東西可以修改應用中資源的加載路徑,如果沒有指定"ui5.walkthrough": "./",那麼加載資源時會加載到/resource下,導致失敗。
相關閱讀:SAP UI5 應用 index.html 裏 data-sap-ui-resourceroots 指令的含義和作用