[英]cant use function into another one from imported file?
Dealing with a sapui5 client app. 处理sapui5客户端应用程序。 I'm trying to use a function from one common js file in another function located in a controller js file but I'm getting a error "is not a function".
我试图在一个位于控制器js文件中的另一个函数中使用一个常见js文件中的函数,但出现错误“不是函数”。
the controler file -> controller/controller.js 控制文件-> controller / controller.js
sap.ui.define([
"path/js/error"
], function(JSONModel) {
"use strict";
onInit: function() {
var type="servidor";
var oModelServidores = new sap.ui.model.json.JSONModel();
oDataModel.read('/ServersSet', {
async: false,
success: function(data) {
//do something
},
error: function(type, err) {
errHandler(type,err); //here I'm getting the error
}
});
}
});
the js file where is the function I want to use in controller -> js/error.js js文件是我想在控制器中使用的功能-> js / error.js
function errHandler(type, err){
var ErrorResponse = err.responseText;
var ErrorResponseBody = JSON.parse(err.responseText);
var msgError = ErrorResponseBody.error.message.value;
switch(type) {
case 'servidor':
console.log('Ups! se ha producido un error a nivel de servidor: ' + msgError);
break;
default:
console.log('Se ha producido un error inesperado');
}
}
Any idea why getting this issue? 知道为什么会遇到这个问题吗?
define
is an AMD convention and it looks like you are dealing with AMD modules. define
是一项AMD惯例,看起来您正在使用AMD模块。 You should read about how AMD works. 您应该阅读有关AMD如何工作的信息。 require.js is one implementation of AMD.
require.js是AMD的一种实现。
The code 编码
sap.ui.define(["path/js/error"], function (JSONModel) {...}
means: "load the file at path/js/error
and populate what it returns into the variable JSONModel
. 意思是:“将文件加载到
path/js/error
,并将返回的内容填充到变量JSONModel
。
The way you have written your code means that JSONModel
is either the reference that contains the function errHandler
(if that is what is defined by path/js/error
) or an Object that has a property that contains errHandler
. 编写代码的方式意味着
JSONModel
是包含函数errHandler
的引用(如果这是由path/js/error
定义的),或者是具有包含errHandler
的属性的Object。
So, your code should look something like this instead: 因此,您的代码应该看起来像这样:
controller/controller.js 控制器/controller.js
sap.ui.define([
"path/js/error"
], function (JSONModel) { // This is where the module defined by path/js/error is stored
"use strict";
onInit: function () {
var type = "servidor";
var oModelServidores = new sap.ui.model.json.JSONModel();
oDataModel.read('/ServersSet', {
async: false,
success: function (data) {
//do something
},
error: function (type, err) {
// Use the errHandler function of the dependency
JSONModel(type, err);
// or if errHandler is attached to an object that is returned
JSONModel.errHandler(type, err)
}
});
}
});
path/js/error.js 路径/js/error.js
define([], function () {
return function errHandler(type, err) {
var ErrorResponse = err.responseText;
var ErrorResponseBody = JSON.parse(err.responseText);
var msgError = ErrorResponseBody.error.message.value;
switch (type) {
case 'servidor':
console.log('Ups! se ha producido un error a nivel de servidor: ' + msgError);
break;
default:
console.log('Se ha producido un error inesperado');
}
}
});
you should import the js file at the beginning and give it a handle. 您应该在开头导入js文件并为其提供句柄。 Your code should look like this:
您的代码应如下所示:
sap.ui.define([
"path/js/error",
"sap/ui/model/json/JSONModel"
], function (Error, JSONModel) {...
...
var oModelServidores = new JSONModel(); <-- note
...
Error.errHandler(type, err) <- you could call the handle to the js file something else
ensure the js file is in relative path you have specified 确保js文件位于您指定的相对路径中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.