在 Magento 2 中所有页面的前端添加 JS 文件的步骤:
步骤1:首先你需要在app\code\[Vendor]\[Module]\view\frontend\layout\default.xml创建default.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">
<head>
<link src="[Vendor]_[Module]::js/js_fileName.js"/>
</head>
</page>
|
步骤2:现在,在 app\code\[Vendor]\[Module]\view\frontend\web\js\js_fileName.js 创建 JS 文件
步骤 3:执行 setup Upgrade 命令和 setup:static-content:deploy 命令,刷新页面即可。