简体   繁体   English

不能从导入的文件中使用功能到另一个功能?

[英]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.

相关问题 Javascript - 覆盖从另一个文件导入的 function - Javascript - Override function imported from another file 调用从另一个文件导入的 function - Call a function imported from another file 将道具传递给从另一个文件导入的功能 - Passing props to function imported from another file 如何使用另一个Javascript文件中的函数? - How can I use a function from one Javascript file in another? 开玩笑 - mocking 从另一个文件导入 function 不起作用 - Jest - mocking imported function from another file not working 如何从 js 文件上的一个 function 获取变量并将其用于另一个 js 文件上的另一个 function? - How do I get a variable from one function on a js file and use it for another function on another js file? 使用从一个组件到另一个组件的异步函数 - Use an async function from one component to another 如何将一个 Function 的结果用于另一个 - How to use result from one Function to another 将值从一个javascript函数转换为另一个 - Use values from one javascript function into another 是否可以导出一个 function 调用另一个 function 在导入模块的文件中定义? - Is it possible to export a function that calls another function defined in the file where the module is imported from?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM