How to get url for static image file in Magento 2's javascript files?
In JavaScript it's available with require.toUrl('images/image.png')
.
I did it by adding variable to window from *.phtml file:
<script>
window.imgpath = '<?php echo $block->getViewFileUrl('images/image.png') ?>';
</script>
and reading that variable from window in *.js:
function someFunction() {
var imgPath = window.imgpath;
}
Actually, in Magento core files I saw examples of such things.