简体   繁体   English

在 Magento2 结帐中覆盖 js 文件

[英]Override js file in the Magento2 checkout

I try to override a js file in the magento2 checkout.我尝试覆盖 magento2 结帐中的 js 文件。

I want to override /vendor/magento/module-checkout/view/frontend/web/js/view/form/element/email.js .我想覆盖/vendor/magento/module-checkout/view/frontend/web/js/view/form/element/email.js

So I copied the file in my module to: /app/code/Myself/Test/view/frontend/web/js/view/form/element/email.js所以我将模块中的文件复制到: /app/code/Myself/Test/view/frontend/web/js/view/form/element/email.js

I did a small change in /app/code/Myself/Test/view/frontend/web/js/view/form/element/email.js :我在/app/code/Myself/Test/view/frontend/web/js/view/form/element/email.js做了一个小改动:

        /**
     * Callback on changing email property
     */
    emailHasChanged: function () {
        var self = this;

        clearTimeout(this.emailCheckTimeout);

        if (self.validateEmail()) {
            quote.guestEmail = self.email();
            checkoutData.setValidatedEmailValue(self.email());
            $.cookie("checkoutemail", self.email()); // <--- this is the change
        }
        this.emailCheckTimeout = setTimeout(function () {
            if (self.validateEmail()) {
                self.checkEmailAvailability();
            } else {
                self.isPasswordVisible(false);
            }
        }, self.checkDelay);

        checkoutData.setInputFieldEmailValue(self.email());
    },

The other parts of the file are unchanged.文件的其他部分不变。

Then I created the /app/code/Myself/Test/view/frontend/requirejs-config.js :然后我创建了/app/code/Myself/Test/view/frontend/requirejs-config.js

var config = {
    map: {
        '*':
            {
                'Magento_Checkout/js/view/form/element/email.js':'Myself_Test/js/view/form/element/email.js'
            }
    }
};

In this requirejs-config.js I'm not sure, where the paths begin.在这个requirejs-config.js我不确定路径从哪里开始。 So I also tried it like this: 'Magento_Checkout/web/js/view/form/element/email.js':'Myself_Test/web/js/view/form/element/email.js' .所以我也这样尝试: 'Magento_Checkout/web/js/view/form/element/email.js':'Myself_Test/web/js/view/form/element/email.js'

But the override don't work.但是覆盖不起作用。 The original email.js is loaded in the checkout.原始 email.js 在结帐中加载。

I did run the setup:upgrade command after changes and my Magento2 Shop is in developer mode, uses the Luma Theme and has only the example data and my Module installed.我确实在更改后运行了setup:upgrade命令,并且我的 Magento2 商店处于开发人员模式,使用 Luma 主题并且只安装了示例数据和我的模块。

I see you are adding file extension to the path.我看到您正在向路径添加文件扩展名。 Remove it and it should work.删除它,它应该可以工作。

var config = {
  map: {
    '*': {                   
      'Magento_Checkout/js/view/form/element/email': 'Myself_Test/js/view/form/element/email'
     }
  }
};

This was the answer for the question.这就是问题的答案。 Below alternative solution.下面的替代解决方案。

Instead of overriding whole file you can extend it and just change part of it.您可以扩展它并只更改其中的一部分,而不是覆盖整个文件。 This way if you will update Magento 2 and there will be change in some other methods that the one you modified you won't need to update your custom file.这样,如果您将更新 Magento 2,并且您修改的其他一些方法将发生变化,您将不需要更新您的自定义文件。

How to extend javascript files?如何扩展javascript文件? In this case uiComponent在这种情况下 uiComponent

https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/javascript/js_mixins.html#examples https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/javascript/js_mixins.html#examples

Change your path from改变你的路径

/app/code/Myself/Test/web/js/view/form/element/email.js /app/code/Myself/Test/web/js/view/form/element/email.js

to

/app/code/Myself/Test/view/frontend/web/js/view/form/element/email.js /app/code/Myself/Test/view/frontend/web/js/view/form/element/email.js

Then clear cache run content deploy and try again.. `然后清除缓存运行内容部署并重试..`

The issue is in the path you are using to do the override.问题出在您用来进行覆盖的路径中。

Change the file location to the path given below and it will work for you.将文件位置更改为下面给出的路径,它将为您工作。

/app/code/Magento/Checkout/view/frontend/web/js/view/form/element/email.js /app/code/Magento/Checkout/view/frontend/web/js/view/form/element/email.js

Don't forget to flush the cache.不要忘记刷新缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM