首页 > 解决方案 > `this.oType.formatValue` 不是函数

问题描述

我正在尝试创建一个自定义类型,如下所示:

return {
    create: function (bSign, sUnit, iDecimal) {
        return sap.ui.model.SimpleType.extend("type.NumUnit", {

            formatValue: function (iValue, sInternalType) {
                if (bSign) {
                    const iNeg = iValue * -1;
                    return `${iNeg} ${sUnit}`;
                }
                return `${iValue} ${sUnit}`;
            },

            parseValue: function (iValue, sInternalType) {
                //Split from unit
                const [iQuantity] = iValue.split(" ");
                return Util.convertStrToFloat(iQuantity, iDecimal);
            },

            validateValue: function (iValue) {
                return iValue;
            }
        });

    }
};

并像这样使用它:

const oNumInput = new sap.m.Input(sId, {
    maxLength: oData.NumberDigits,
    type: sap.m.InputType.Number,
    showValueHelp: oData.WithValues
});

const oType = UnitType.create(oData.Sign, oData.UnitTExt, oData.NumberDecimals);

oNumInput.bindProperty("value", {
    path: "value",
    type: oType
});
return oNumInput;

编译器抱怨:

PropertyBinding-dbg.js:91 Uncaught (in promise) TypeError: this.oType.formatValue is not a function
    at constructor.P._toExternalValue (PropertyBinding-dbg.js:91)
    at constructor.P.getExternalValue (PropertyBinding-dbg.js:77)
    at f.g.updateProperty (ManagedObject-dbg.js:3367)
    at constructor.v (ManagedObject-dbg.js:3209)
    at constructor.a.fireEvent (EventProvider-dbg.js:228)
    at constructor.B._fireChange (Binding-dbg.js:271)
    at constructor.O.checkUpdate (ODataPropertyBinding-dbg.js:133)
    at constructor.O.initialize (ODataPropertyBinding-dbg.js:48)
    at f.g._bindProperty (ManagedObject-dbg.js:3310)
    at f.g.updateBindings (ManagedObject-dbg.js:4049)

我究竟做错了什么?

标签: sapui5

解决方案


类型错误:this.oType.formatValue

当分配的类型不是sap.ui.model.Type.

根据您的代码,SimpleType.extend返回一个您仍然必须从中创建实例的模块。因此,您不能只将模块分配给type:when 绑定属性。您仍然需要使用以下命令调用构造函数new

const Type = UnitType.create(oData.Sign, oData.UnitTExt, oData.NumberDecimals);
oNumInput.bindProperty("value", {
  path: sPath, /*E.g.: "CharInput>/ZPM_TEST_7" and NOT a static value*/
  type: new Type()
});

然后,创建一个应该具有该formatValue方法的新实例。


推荐阅读