How to create Magento 2 custom module with include Css and Js ?
2 Answers
I Hope this is useful for You,
add your css file to
app/code/vendor/module/view/frontend/web/css/mycss.css
mycss.css
body{
background-color:green;
}
add your Js file
app/code/vendor/module/view/frontend/web/js/myjs.js
myjs.js:
require([
"jquery"
], function($){
$(document).ready(function() {
alert("Hello");
});
});
Reference: http://webkul.com/blog/magento2-how-to-add-css-and-js-file-in-module/ http://www.webspeaks.in/2016/03/how-to-add-css-and-js-in-magento-2-custom-module.html
Then add css and Js File to Your layout File.
app/code/vendor/module/view/frontend/layout/test_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="Vendor_Module::css/mycss.css" />
<script src="Vendor_Module::js/myjs.js"/>
</head>
<body>
<referenceContainer name="content">
<block class="Vendor\Module\Block\class" template="Vendor_Module::class.phtml"/>
</referenceContainer>
</body>
</page>
Remove this folder
rm -rf var/cache/*
rm -rf var/page_cache/*
Remove pub/static
rm -rf pub/static/*
or
If throw any error, via remove pub/static in terminal,
Go to <Magento root Dir> via file manager the remove pub/static directory.
not using terminal to remove pub/static folder.
Next generate Static Content:
bin/magento setup:static-content:deploy
Or
php bin/magento setup:static-content:deploy
Now, try, Its worked for me.
- 295
- 3
- 14
- 3,570
- 7
- 38
- 73
Try this
Create the view/<area>/layout/default.xml file in your module folder, which can be app/code/Df/Core, for instance.
Replace <area> with adminhtml or frontend
default.xml should look like:
<?xml version="1.0"?>
<page
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
layout="admin-1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"
>
<head>
<link src="Df_Core::core.js"/>
<css src="Df_Core::core.css"/>
</head>
<body>
<body/>
</page>
put the core.js and core.css files to the view/<area>/web folder.
phpbeforebin/magento setup:static-content:deploySo, last command is:
– WaPoNe Dec 01 '16 at 15:11php bin/magento setup:static-content:deploybin/magento setup:static-content:deployits is also works , i think windows only usephp bin/magento setup:static-content:deploy– Rajkumar .E Dec 01 '16 at 15:23<css src="vendorName_moduleName::css/mycss.css" />and<script src="vendorName_moduleName::js/myjs.js"/>– WaPoNe Dec 01 '16 at 15:59rm -rf pub/staticfor apache users, yourpub/static/.htaccessfile may be removed causing console logs about missing files. – Dave Jan 19 '17 at 01:49rm -rf pub/static, if got issue while remove using rm -rf pub/static command , try to remove vis file manager not using terminal. – Rajkumar .E Jan 19 '17 at 04:44<css src="Module_Name::css/mycss.css"/>– crashtestxxx Nov 30 '19 at 03:42<block class="Vendor\Module\Block\class" name="some-name" template="Vendor_Module::class.phtml"/>– crashtestxxx Nov 30 '19 at 03:47