Magento2 如何获取Magento 2'中静态图像文件的url;什么是javascript文件?

Magento2 如何获取Magento 2'中静态图像文件的url;什么是javascript文件?,magento2,Magento2,我需要从javascript文件(payment.js)获取存储在我的主题中的图像的URL(app/design/frontend/MyVendor/MyVendor/MyTheme/web/images/image.png) 在PHP中,我可以这样做: <?php echo $block->getViewFileUrl('images/image.png') ?> 如何在JavaScript中实现这一点?我通过从*.phtml文件向窗口添加变量来实现: <scri

我需要从javascript文件(
payment.js
)获取存储在我的主题中的图像的URL(
app/design/frontend/MyVendor/MyVendor/MyTheme/web/images/image.png

在PHP中,我可以这样做:

<?php echo $block->getViewFileUrl('images/image.png') ?>


如何在JavaScript中实现这一点?

我通过从*.phtml文件向窗口添加变量来实现:

<script>
    window.imgpath = '<?php echo $block->getViewFileUrl('images/image.png') ?>';
</script>

实际上,在Magento core文件中,我看到了这样的例子。

更好的方法是:

.phtml文件

<script type="text/x-magento-init">
    {
        "*": {
            "Module/js/example":"<?php echo "test" ?>"
        }
    }        
</script>

Doc:

在JavaScript中,它可以通过
require.toUrl('images/image.png')
格式,删除了不相关的标记,不知道为什么会被否决
<script type="text/x-magento-init">
    {
        "*": {
            "Module/js/example":"<?php echo "test" ?>"
        }
    }        
</script>
define([], function () {
    var mageJsComponent = function(config)
    {
        console.log(config);
    };

    return mageJsComponent;
});