I have a JS file that is loaded through require-js.config
.
I need to get access to a store->configurations
value inside my JS file.
Does anyone know of a way to accomplish that?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIf we take a look at onepage phtml template, we can see how to set and get a global config Js variable.
vendor/magento/module-checkout/view/frontend/templates/onepage.phtml
......
<script>
window.checkoutConfig = <?php /* @escapeNotVerified */ echo \Zend_Json::encode($block->getCheckoutConfig()); ?>;
// Create aliases for customer.js model from customer module
window.isCustomerLoggedIn = window.checkoutConfig.isCustomerLoggedIn;
window.customerData = window.checkoutConfig.customerData;
</script>
......
We can do the same way by setting a global Js variable.
For example, in your template:
<script>
window.valuesConfig = '<?php /* @escapeNotVerified */ echo $block->getValuesConfig(); ?>'
</script>
And then, in your custom js, you can get:
getValues: function() {
return window.valuesConfig;
}
Remember add the method in your block, for example:
public function getValuesConfig()
{
return $this->_scopeConfig->getValue('get/value/config');
}