1

How can we get Magento 2 base URL in web template (.html file) ?

Note: I am talking about web template (.html file). Not about general templates file (.phtml file).

sv3n
  • 11,657
  • 7
  • 40
  • 73
Kishan Patadia
  • 5,609
  • 3
  • 24
  • 36

1 Answers1

3

=> Set URL in the JS file :

First of all, Add mage/url to the list of dependencies.

Then, Add the following function to the knockout JS file:

getBaseUrl: function() {
    return url.build('<modulename>/<controllername>/<actionname>');
},

=> Use Knockout to set the href :

<a data-bind="attr: { href: getBaseUrl() }">Link Text</a>

Then, clear Varnish and browser cache.

Rohan Hapani
  • 17,388
  • 9
  • 54
  • 96