I found an easy hello world example from Max Pronko which uses text/x-magento-init to define a uiComponent.
MODULE/view/frontend/templates/script.phtml
<div data-bind="scope: 'pronko-component'">
<h2 data-bind="text: title"></h2>
</div>
<script type="text/x-magento-init">
{
"*": {
"Magento_Ui/js/core/app": {
"components": {
"pronko-component": {
"component": "Pronko_UiComponent/js/component",
"title": "Hello World!"
}
}
}
}
}
</script>
MODULE/view/frontend/layout/default.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="pronko.uicomponent"
template="Pronko_UiComponent::script.phtml" />
</referenceContainer>
</body>
</page>
MODULE/view/frontend/web/js/component.js
define([
'uiElement'
], function(Component) {
'use strict';
return Component.extend();
});
This shows "Hello world" in the frontend.
How can I rewrite this, to use XML uiComponent instead?
I tried it like this:
Company/Shipping/view/frontend/templates/test.phtml
<div data-bind="scope: 'foobar'">
<h2 data-bind="text: title"></h2>
</div>
Company/Shipping/view/frontend/web/js/test.js
define([
'uiElement'
], function(Component) {
'use strict';
return Component.extend();
});
Company/Shipping/view/frontend/layout/default.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="company.shipping.test" template="Company_Shipping::test.phtml">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="foobar" xsi:type="array">
<item name="component" xsi:type="string">Company_Shipping/js/test</item>
<item name="title" xsi:type="string">Hello world!</item>
</item>
</item>
</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
But it does not write any data into the <h2>
where it should put the title data "Hello world!" into it.
I've read the official documentation but the topic is a chaos with no practical examples.