Magento – Magento 2 – Add success message with knockout

knockoutjsmagento-2.1magento2messages

I need add an errorMessage or successMessage on checkout/cart after an jquery validation. I tried require file 'Magento_Ui/js/model/messageList' for this but not have success.

My code:

define(
    [
        'jquery',
        'Magento_Ui/js/model/messageList'
    ],
    function ($, messageList) {
       $.ajax({
           type: "GET",
           url: /route/controller,
           async: false,
           success: function (res) {
               if (res.val) {
                  messageList.addErrorMessage({'message': messageError});
               }
           }
       });
    }
);

Best Answer

I'm not sure if you already solved this or not, but here it goes anyway. While Rakesh might be right (a controller is the preferred way), it doesn't answer the question. I found the easiest way of doing this is through the customerData object:

customerData.set('messages', {
    messages: [{
        type: 'success',
        text: 'Hello World'
    }]
});
Related Topic