How do I add messages using Magento 2 Ui messages to page via javascript from a custom module?
I have found a file in Magento/Ui/view/frontend/web/js/view/messages.js that appears to be responsible for adding messages to the page, in the same manner as the php message manager interface.
Am I mistaken? If not, how can it be used to add errors and success messages like the php version?
Best Answer
Looks like you're on the right way. You just need to perform the following simple steps in order to get it working.
Add the messages container as a child into your component. It can be done via js layout config.
Example: Magento_Customer::view/frontend/layout/default.xml
Then add the messages block into a place where you expect to see the messages within your custom KnockoutJS template:
Example: Magento_Customer::view/frontend/web/template/authentication-popup.html
Add the messages list dependency into your js file and call the corresponding function:
addSuccessMessage
oraddErrorMessage
.Example: Magento_Customer::view/frontend/web/js/action/login.js