Magento 2 custom js file in require js in custom module

Add custom mycustom.js file into magento 2

Place requirejs-config.js file in your module at path app/code/Denish/Example/view/frontend


var config = {
    map: {
        '*': {
            mycstomjsfile: 'Denish_Example/js/mycustom',
        }
    }
};

map :- maps your file with the alias name which you defined in the object.

Now add js file


app/code/Denish/Example/view/frontend/web/js/mycustom.js


we can use with code to test

require([
        'jquery',
        'mycstomjsfile'
    ], function ($, script) {
        //Your code
        alert('Here');
    });


Now remove cache and deploy static content. refer previous post i have posted in MAGENTO 2 BASIC COMMANDS.

Check your console mycustom.js will be loaded.


Easy one...!!!


Experienced Magento Certified Developer with a demonstrated history of working in the information technology and services industry. Skilled in SQL, PHP, XAMPP, jQuery, and Microsoft Office. Strong engineering professional with a Master of Computer Applications

0 comments:

Post a Comment

Popular Posts

Have You Any Questins Let Us Know.

Contact Us
DENISH VACHHANI
Ahmedabad India