2

I am creating a custom module which retrieves the image from Package_Module/images/image.jpg in my javascript code. How to get the require js base URL or Static URL? or do you have any suggestions on what is the best to get the link of an image asset from javascript level?

Ner
  • 1,071
  • 12
  • 28

2 Answers2

6

You can pass it through using x-magento-init as Khoa mentioned, or you can use this:

require.toUrl('test');

Which will output:

//localhost:3000/static/frontend/Vendor/theme/en_GB/test

Where localhost:3000 is your base URL.

Ben Crook
  • 15,685
  • 3
  • 52
  • 104
-2

1) Using the Js global variable. For example, on the checkout page, we can check:

window.checkoutConfig.imageData

enter image description here

See more here: https://magento.stackexchange.com/a/133732/33057

2) Passing the image to Js variable in your phtml:

<script type="text/x-magento-init">
    {
        "[data-block='minicart']": {
            "Magento_Ui/js/core/app": <?= /* @escapeNotVerified */ $block->getJsLayout() ?>
        },
        "*": {
            "Magento_Ui/js/block-loader": "<?= /* @escapeNotVerified */ $block->getViewFileUrl('images/loader-1.gif') ?>"
        }
    }
</script>
Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155