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?
-
1please provide more detail to your questionMurtuza Zabuawala– Murtuza Zabuawala ♦2016年09月26日 13:52:51 +00:00Commented Sep 26, 2016 at 13:52
-
Hi @LM_Fielding, did you manage to find a solution for this issue? I've stumbled upon 'Magento_Ui/js/model/messageList' myself but I can't seem to get it to work. This is what I did: define(['Magento_Ui/js/model/messageList'], function(messageList) { messageList.addErrorMessage({ message: 'Message to be shown.' }); }); But the message is never displayed.Magarusu– Magarusu2018年03月28日 10:28:49 +00:00Commented Mar 28, 2018 at 10:28
1 Answer 1
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.
<item name="children" xsi:type="array"> <item name="messages" xsi:type="array"> <item name="component" xsi:type="string">Magento_Ui/js/view/messages</item> <item name="displayArea" xsi:type="string">messages</item> </item> </item>Then add the messages block into a place where you expect to see the messages within your custom KnockoutJS template:
<!-- ko foreach: getRegion('messages') --> <!-- ko template: getTemplate() --><!-- /ko --> <!--/ko-->Example: Magento_Customer::view/frontend/web/template/authentication-popup.html
Add the messages list dependency into your js file and call the corresponding function:
addSuccessMessageoraddErrorMessage.define([ 'Magento_Ui/js/model/messageList' ], function (messageList) { messageList.addSuccessMessage({ message: 'Message to be shown.' }); });Example: Magento_Customer::view/frontend/web/js/action/login.js
-
shall I do the same thing for backendJaisa– Jaisa2019年03月08日 03:54:20 +00:00Commented Mar 8, 2019 at 3:54
Explore related questions
See similar questions with these tags.