我正在尝试重写以下函数 postcodeValidation:
vendor/magento/module-checkout/view/frontend/web/js/model/shipping-rates-validator.js
我已经能够创建一个 mixin,但我不确定如何更改现有函数。我想要更改验证消息(不使用 CSV 文件)。
这是我的 mixin 中的代码:
define(['mage/utils/wrapper', 'mage/translate', 'jquery'],function (wrapper, $t, $){
'use strict';
return function (target) {
var postcodeValidation = wrapper.wrap(target.postcodeValidation, function(originalFunction, config, element){
originalFunction();
warnMessage = $t('test');
});
target.postcodeValidation = postcodeValidation;
return target;
};
});
现在这不起作用,因为 warnMessage 未定义。我需要复制整个函数吗?
最佳答案
请尝试这个:
define(['mage/utils/wrapper'], function (wrapper) {
'use strict';
return function (shippingRatesValidator) {
shippingRatesValidator.postcodeValidation = wrapper.wrapSuper(shippingRatesValidator.postcodeValidation, function (postcodeElement) {
this._super(postcodeElement);
console.log('aaaaaaaaaaaaa');
// add extended functionality here or modify method logic altogether
});
return shippingRatesValidator;
};});
关于javascript - 如何使用 mixin 覆盖这个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73388617/