【问题标题】:How to overwrite this function using a mixin?如何使用 mixin 覆盖这个函数?
【发布时间】:2022-08-24 12:22:32
【问题描述】:

我正在尝试覆盖以下函数 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 未定义。我需要复制整个函数吗?

    标签: javascript jquery magento requirejs magento2


    【解决方案1】:

    请试试这个:

    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;
    };});
    

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 1970-01-01
      • 2014-09-08
      • 2012-09-24
      • 1970-01-01
      • 2012-10-28
      • 2015-03-31
      • 1970-01-01
      • 2020-04-10
      相关资源
      最近更新 更多