php - Magento 2 - 扩展 onepage.phtml 以添加自定义 JS
问题描述
Magento 2
我创建了一个自定义送货方式模块,需要在设置送货地址和送货方式的结帐页面中添加自定义 JS。但这对我不起作用。
我onepage.phtml
在以下位置添加了我自己的:
My_Module\view\frontend\templates\onepage.phtml
我还在checkout_index_index.xml
以下位置创建了一个文件:
My_Module\view\frontend\layout\checkout_index_index.xml
我的 JS 的位置:
My_Module\view\frontend\web\js\custom.js
我的onepage.phtml
文件内容:
<script type="text/javascript">
require(['jquery', 'custom-js'], function($) {
alert('JS loaded');
});
</script>
我的checkout_index_index.xml
文件内容:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="checkout.onepage">
<action method="setTemplate">
<argument name="template" xsi:type="string">My_Module::onepage.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
我的requirejs-config.js
文件内容:
var config = {
map: {
'*': {
custom-js: 'My_Module/js/custom-js'
}
}
};
我的module.xml
文件内容:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="My_Module" setup_version="1.0.1">
<sequence>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
当我将onepage.phtml
文件的内容添加到核心onepage.phtml
文件时,JS 会正确加载。
另外,当我在文件中referenceBlock
name
更改为时,我的 JS 文件被加载,但似乎只有文件被渲染,没有别的......我使用了错误的值吗?checkout.root
checkout_index_index.xml
onepage.phtml
name
有没有更好的方法来包含 JS 或者我只是错过了什么?任何帮助,将不胜感激!
解决方案
推荐阅读
- python - ValueError:无法将字符串转换为浮点数:sklearn、numpy、panda
- python - Django 模型抽象基类之间的 ForeignKey
- jquery - AJAX partial loading page inside div problem
- typescript - 如何内联快速初始化打字稿数据结构
- html - 无法通过 VBA 在 Internet Explorer 中选择下拉列表
- node.js - 如何实现一个管理员审批系统,用于在 node.js 中发布博客并表达应用程序?
- c++ - 设置 viewController 共享公共对象
- nativescript - 如何使用 Appium 将 browserstack 与 Nativescript 集成?
- python - 使用 Python 实现最小堆
- flutter - 如何在 Flutter 中制作 Arc(Curve)轮播(ListVie,Row Widget)?