如何与SAPUI5一起使用第三方JS库



你好,我正在尝试使用PapaParse库将csv文件中的内容读取到数组中。我不知道如何使用控制器中的库。我在我的项目中添加了papaparse.min.js文件夹,并在我的控制器中定义了它,但它仍然不起作用。有人能给我一些建议吗?我应该怎么做才能让这样的第三方库与UI5应用程序一起工作?

我一步一步地遵循了本教程,但似乎无法使其发挥作用-->https://blogs.sap.com/2017/04/30/how-to-include-third-party-libraries-modules-in-sapui5/

sap.ui.define([
"sap/ui/core/mvc/Controller",
"cap/trn/Amar_Billing/libs/papaparse"
], function (Controller, PapaParse) {
"use strict";
return Controller.extend("cap.trn.Amar_Billing.controller.Home", {

onInit: function () {
this.oRouter = this.getOwnerComponent().getRouter();
},


handleUploadFile: function(oEvent) {
//var that = this;
var oFileToRead = sap.ui.getCore().byId("fileUploader").getFocusDomRef().files[0];

PapaParse.parse(oFileToRead, {
download:true,
header: true,
skipEmptyLines: true,
complete: function(results){
console.log(results);
}
});

});
});
<core:FragmentDefinition xmlns:core="sap.ui.core" 
xmlns="sap.m"
xmlns:f="sap.ui.layout.form"
xmlns:u="sap.ui.unified">
<u:FileUploader
id="fileUploader"
uploadUrl="upload/"
tooltip="upload your billing request"
uploadComplete="handleUploadComplete" />
<Button text="Upload File"
press="handleUploadFile" />
</core:FragmentDefinition>

项目的文件结构

按下上传按钮时控制台出现错误

控制器中的变量PapaParseundefined,因为PapaParse不像UI5那样使用相同的模块体系结构。你的链接文章中也提到了这一点。

全局变量名为Papa,因此请尝试调用Papa.parse

全局名称和导入的名称不同是很重要的,否则导入将用undefined覆盖全局。

/* global Papa:true */
sap.ui.define([
"sap/ui/core/mvc/Controller",
"cap/trn/Amar_Billing/libs/papaparse"
], function (Controller, PapaParse) { // Use any name for your lib here (but not the name of the global variable)
Papa.parse(...); // Use the global variable here.
});

最新更新