Magento 2 – My JS File is Not Loaded

javascriptjquerymagento2requirejs

My steps:

1) Load my file slider.js to: app/design/frontend/Vendorname/default/web/js.

2) Create requirejs-config.js to: app/design/frontend/Vendorname/default

var config = {
        paths: {
            slider:        "js/slider"
        },
        shim: {
            slider: {
                deps: ["jquery"]
            }
        }
    }; 

3) rm -R pub/static/*

4) bin/magento setup:static-content:deploy

When I open site, slider.js is not loaded.
I need to call him, that he began to load?
I read a lot of Magento 2: Javascript Init Scripts, Use custom JavaScript.

Realy need help.

Best Answer

The Way you are including custom js in requirejs-config-js is correct , but you must need to load this js in your template(.phtml) file.

Add below code in your .phtml template file :

<script type="text/javascript">

    require(['jquery', 'slider'],function($){
        (function() {
             //code goes here
        })(jQuery);
    });
</script>