{"id":7908,"date":"2021-05-02T13:38:55","date_gmt":"2021-05-02T13:38:55","guid":{"rendered":"https:\/\/www.bay20.com\/?p=7908"},"modified":"2025-05-21T16:28:39","modified_gmt":"2025-05-21T10:43:39","slug":"anleitung-zum-hinzufugen-von-benutzerdefiniertem-javascript-in-agento-2-mit-require-js","status":"publish","type":"post","link":"https:\/\/www.bay20.com\/de\/anleitung-zum-hinzufugen-von-benutzerdefiniertem-javascript-in-agento-2-mit-require-js\/","title":{"rendered":"Wie man benutzerdefinierte Javascript in Magento 2 mit requireJS hinzuf\u00fcgen?"},"content":{"rendered":"\n<p>Erstellen Sie ein neues Modul mit Bay20_Bay20CustomJS<\/p>\n\n\n\n<p><strong>Schritt 1:<\/strong> Erstellen Sie die Datei <strong>registration.php.<\/strong><\/p>\n\n\n\n<p>app\/code\/Bay20\/Bay20CustomJS\/registration.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n\t\\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n\t'Bay20_Bay20CustomJS',\n\t__DIR__\n);<\/code><\/pre>\n\n\n\n<p><strong>Schritt 2:<\/strong> Erstellen Sie die Datei <strong>module.xml.<\/strong><\/p>\n\n\n\n<p>app\/code\/Bay20\/Bay20CustomJS\/etc\/module.xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\"?&gt;\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;\n    &lt;module name=\"Bay20_Bay20CustomJS\" setup_version=\"1.0.0\"&gt;\n    &lt;\/module&gt;\n&lt;\/config&gt;<\/code><\/pre>\n\n\n\n<p><strong>Schritt 3:<\/strong> Erstellen Sie die Datei <strong>requirejs-config.js.<\/strong><\/p>\n\n\n\n<p>app\/code\/Bay20\/Bay20CustomJS\/view\/frontend\/requirejs-config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var config = {\n    map: {\n        '*': {\n            wktestrequire: 'Bay20_Bay20CustomJS\/js\/wkrequirejs',\n        }\n    }\n};<\/code><\/pre>\n\n\n\n<p><strong>Schritt 4:<\/strong> Datei <strong>test.phtml<\/strong> erstellen<\/p>\n\n\n\n<p>app\/code\/Bay20\/Bay20CustomJS\/view\/frontend\/templates\/test.phtml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=\"testdiv\"&gt;\n\t&lt;a&gt;Test anchor tag&lt;\/a&gt;\n&lt;\/div&gt;\n\n&lt;?php\n$formData = &#91;\n    'divElement'       =&gt;  '#testdiv',\n];\n$serializedFormData = $this-&gt;helper('Magento\\Framework\\Json\\Helper\\Data')-&gt;jsonEncode($formData);\n?&gt;\n&lt;script type=\"text\/x-magento-init\"&gt;\n    {\n        \"*\": {\n            \"wktestrequire\": &lt;?php \/* @noEscape *\/ echo $serializedFormData; ?&gt;\n        }\n    }\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p><strong>Schritt 5:<\/strong> Erstellen Sie die Datei <strong>wkrequirejs.js.<\/strong><\/p>\n\n\n\n<p>app\/code\/Bay20\/Bay20CustomJS\/view\/frontend\/web\/js\/wkrequirejs.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>define(&#91;\n    \"jquery\",\n    'Magento_Ui\/js\/modal\/alert',\n    \"jquery\/ui\",\n], function ($, alert) {\n    'use strict';\n    $.widget('mage.wktestrequire', {\n        options: {\n            confirmMsg: ('divElement is removed.')\n        },\n        _create: function () {\n            var self = this;\n            $(self.options.divElement).remove();\n            alert({\n                content: self.options.confirmMsg\n            });\n        }\n    });\n    return $.mage.wktestrequire;\n});<\/code><\/pre>\n\n\n\n<p><strong>Schritt 6:<\/strong> F\u00fchren Sie den folgenden Befehl aus.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php bin\/magento setup:upgrade<\/code><\/pre>\n\n\n\n<p><strong>Bitte kontaktieren Sie uns unter <strong><a href=\"mailto:manish@bay20.com\">manish@bay20.com<\/a><\/strong> oder rufen Sie uns unter <strong><a href=\"https:\/\/api.whatsapp.com\/send?phone=+918800519180&amp;text=Hi,%20I%20contacted%20you%20through%20your%20website.\" target=\"_blank\" rel=\"noreferrer noopener\">+91-8800519180<\/a><\/strong> an, wenn Sie Unterst\u00fctzung f\u00fcr Magento 2 ben\u00f6tigen. Sie k\u00f6nnen auch die <strong><a href=\"https:\/\/www.bay20.com\/de\/magento-entwicklungsunternehmen\/\">Magento2<strong>-Entwicklungsseite<\/strong><\/a><\/strong> besuchen, um die von uns angebotenen Dienstleistungen zu \u00fcberpr\u00fcfen.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 uses RequireJS for its JavaScript library. To add custom JavaScript to Magento 2, you must create a RequireJS-compatible JavaScript file, and then add it to the page via the layout XML.<\/p>\n","protected":false},"author":1,"featured_media":8429,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77,79],"tags":[],"class_list":["post-7908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento2","category-magento-anleitung"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/posts\/7908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/comments?post=7908"}],"version-history":[{"count":7,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/posts\/7908\/revisions"}],"predecessor-version":[{"id":17291,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/posts\/7908\/revisions\/17291"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/media\/8429"}],"wp:attachment":[{"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/media?parent=7908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/categories?post=7908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bay20.com\/de\/wp-json\/wp\/v2\/tags?post=7908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}