3

I have created a custom theme in app/design/frontend/Vendor/Themename

In theme directory I have Themename/requirejs-config.js

var config = {
 deps: [
 "js/custom",
 ]
};

Themename/web/js/custom.js

require([
 'jquery'
], function ($) {
 console.log('adsa');
 console.log(config);
});

I have a phtml file included and I am passing php variable to external js

<script type="text/x-magento-init">
{
 "*": {
 "js/custom": {
 "contacturl": "<?php echo $block->getUrl("contact/index/post"); ?>"
 }
 }
}
</script>

This is the error I am getting Config is not defined.

enter image description here

I am in the developer mode What I tried

Cleared Cache

Cleared Var Directory

asked Jul 9, 2018 at 12:00
1
  • please share your custom JS code and I presuming that you are trying to pass contacturl to custom JS. Commented Jul 9, 2018 at 12:39

2 Answers 2

3

I got this working using this

define([
 'jquery'
], function ($) {
 console.log('adsa');
 return function (config) {
 console.log(config.contacturl);
 }
});

Though I am not sure why its not working with require.

answered Jul 9, 2018 at 12:48
2
  • Hi, are you passing contacturl to custom js file? are you trying to call contacturl in custom.js? I want to do same can you tell me your purpouse of doing this? Commented Jul 9, 2018 at 13:17
  • 1
    Yes Jack I am passing the url to external js custom.js. The purpose for this is to use the url in js file generated by php. Commented Jul 9, 2018 at 13:34
1

Although the OP has the answer, but I want to emphasize one thing here:

define([
 'jquery'
], function ($) {
 console.log('adsa');
 return function (config) {
 console.log(config.contacturl);
 }
});

In the above snippet, it MUST be "define"

If you use "require", you won't get what you want. And it's very hard to debug because there is no error.

One more thing, you can name the variable "config" above anything you want. It doesn't have to be exact word "config".

It can be "data", "request", "abc", ...

And from this variable, you can get what you put in "text/x-magento-init"

answered Sep 27, 2018 at 15:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.