I was facing one big issue to access base URL of store in custom module through jQuery object in magento 2.
In PHP, generally it can obtain using $this->getUrl('module/controller/action'); but in jQuery object how we can obtain same ?
In checkout module we seems, there is usage of this.getUrl() method. And its working fine.
After giving hours of dedication I found the solution. and It will be helpful to you.
1 Answer 1
In your jQuery file add 'mage/url' and create a object of it 'urlBuilder'.
e.g.
define(
[
'jquery',
'underscore',
'mage/url',
'jquery/ui'
], function (,ドル _,urlBuilder) {
'use strict';
getCustomUrl: function() {
return urlBuilder.build('/routeName/controller/actioName');
}
});
And use getCustomUrl() function in this jQuery class/widget anywhere.
As per your requirement you can modified it as your best.