requirejs-config.jsutilizza per la creazione della mappatura delle risorse JavaScript . Possiamo trovare tutti richiedono configurazioni sotto: pub/static/_requirejs.
Per quanto ne so, il modo corretto di caricare il nostro script personalizzato tramite Require Js: utilizzando il modello per chiamare il nostro script . Creeremo un nuovo modello con la Magento\Framework\View\Element\Templatesua classe di blocco.
Se vogliamo caricare i file js su tutte le pagine e non vogliamo creare un nuovo modulo, il nostro blocco dovrebbe fare riferimento a before.body.endoafter.body.start container in default.xml- Modulo tema Magento.

app / design / frontend / Venditore / a tema / Magento_Theme / layout / default.xml
<?xml version="1.0"?>
<page layout="3columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceContainer name="after.body.start">
<block class="Magento\Framework\View\Element\Template" name="custom.js" template="Magento_Theme::custom_js.phtml"/>
</referenceContainer>
</page>
app / design / frontend / Venditore / a tema / requirejs-config.js
var config = {
map: {
'*': {
customScript:'Magento_Theme/js/customscript'
}
}
};
app / design / frontend / Venditore / a tema / Magento_Theme / web / js / customscript.js
define('jquery', function($) {
//Your code here
//alert('Here');
}(jQuery)
);
Il nostro modello chiamerà il nostro script: app / design / frontend / Vendor / Theme / Magento_Theme / templates / custom_js.phtml
<script>// <![CDATA[
require([
'jquery',
'customScript'
], function ($, script) {
//Your code here
//alert('Here');
});
// ]]>
</script>
Svuota la cache Magento ed esegui la distribuzione di contenuto statico: php bin/magento setup:static-content:deploy