Does someone knows why it doesn't work ?
app/code/Vendor/Module/view/frontend/requirejs-config.js
var config = {
map: {
'*': {
module: 'Vendor_module/js/go',
}
}
};
console.log('foo'); // i get well "foo" in console
app/code/Vendor/Module/view/frontend/web/js/go.js
define(['jquery'], function($){
"use strict";
return function hello()
{
alert('hello from function');
}
});
app/code/Vendor/Module/view/frontend/templates/file.phtml
<script type="text/javascript">
require(['jquery', 'module'], function(,ドル hello) {
hello();
});
</script>
2 Answers 2
I think you need to delete var/view_preprocessed and run a php bin/magento setup:static-content:deploy (or deploy static assets via your workflow) as it works fine for me.
I wouldn't recommend running JS directly in a PHTML template though, the best practice is to use either data-mage-init or x-magento-init as described in the Dev Docs here.
-
Thanks for your advice but still the same problem, i deployed the static-content even im in developer mode, basically we dont need that in developer mode, and i clean the cache but nothing happen !2018年03月02日 17:14:31 +00:00Commented Mar 2, 2018 at 17:14
-
In your PHTML file if you add
console.log(hello);what is the output?Ben Crook– Ben Crook2018年03月03日 12:43:36 +00:00Commented Mar 3, 2018 at 12:43 -
Inside
require(['jquery', 'module'], function(,ドル hello) { ...I dont get it, in outside that something like this:<script type="text/javascript"> console.log('hello'); </script>i get it2018年03月03日 12:56:46 +00:00Commented Mar 3, 2018 at 12:56 -
1Ah okey, thank you for you and especially for your time. I added you in linkdin.2018年03月03日 14:35:35 +00:00Commented Mar 3, 2018 at 14:35
-
1I just asked this question if you want to answer magento.stackexchange.com/questions/218896/…2018年03月19日 13:24:31 +00:00Commented Mar 19, 2018 at 13:24
I always use the following layout in the requirejs-config.js file:
var config = {
map: {
'*': {
module: "Vendor_module/js/go",
}
},
shim: {
'module': {
deps: ['jquery']
}
},
deps: [
"js/go"
]
};
-
The same issue, now when you added
deps: [ "js/go" ]i still have a new error :Error: Script error for: js/goand a warningfail loading js/go.js2018年03月02日 17:23:26 +00:00Commented Mar 2, 2018 at 17:23
Explore related questions
See similar questions with these tags.