Home > Web Front-end > JS Tutorial > body text

Add custom JS under Magento2 module

不言
Release: 2018-03-30 17:34:44
Original
2245 people have browsed it

This article will share with you about adding custom JS under the Magento2 module. The steps are also relatively clear. Friends in need can refer to it

Let’s take a look at what to do first

1. Self-written or third-party JS files

Add your own files in the following location:

<Vendor>/<Module_Name>/view/<area>/web/js/
Copy after login

Example: //view/< area>/web/js/my.js

2. Configuration file

Add requirejs-config.js file for configuration reference
Add to

<Vendor>/<Module_Name>/view/<area>/
Copy after login

Example: <Vendor>/<Module_Name>/view/<frontend>/requirejs-config.js

var config = {
    map: {
        '*': {
            myjs: 'VendorName_ModuleName/js/my'
        }
    }
};
Copy after login

Here myjs calls the subsequent requirejs

3 .Modify the phtml template

and then you can call it like this

require(['jquery','myjs'],function($){
    //myjs functions
}
Copy after login

4. Directory structure

Borrow other people’s pictures

Add custom JS under Magento2 module


Related recommendations:

What kind of configuration is required for a magento2 website with 100,000 SKUs?

Magento20 How to set the path prompt information of the front page?

magento2 Add payment method payment method

The above is the detailed content of Add custom JS under Magento2 module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!