I have a requirement where I need to display my toast message in multiple lines in my LWC. I tried using \n and
but they are not working. Please assist.
-
2This question is similar to: How to display toast message in multiple lines using LWC. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.– Tushar JadavCommented Jul 26 at 4:30
Add a comment
|
2 Answers
For Multiple Lines Toast in LWC
we have to override CSS :
- Create a CSS file and add in static resource
File Content :
.toastMessage{
white-space: break-spaces !important;
}
- Import That static resource file in your component
import { loadStyle } from 'lightning/platformResourceLoader';
import CUSTOMCSS from '@salesforce/resourceUrl/{yourfileName}';
a) Define variable like : isCssLoaded = false;
- Call it in reRenderCallback
renderedCallback(){
if(this.isCssLoaded) return
this.isCssLoaded = true;
loadStyle(this,CUSTOMCSS).then(()=>{
console.log('loaded');
})
.catch(error=>{
console.log('error to load');
});
}
4)Create your toast message like this :
var msg = 'This is first-line \nThis is Second Line. \nThis is the third Line.';
const evt = new ShowToastEvent({
title: "Complete Required Fields :",
message: msg,
variant: "error",
});
this.dispatchEvent(evt);
-
why directly adding the mentioned css to the css file in lwc will not work? Commented Jul 22, 2022 at 3:09
-
1for overriding LWC standard component CSS we need to import CSS file. Commented Jul 23, 2022 at 13:33
Have a read of this article on SFDC Monkey that covers this exact issue! (I've used this and it works great)
http://sfdcmonkey.com/2018/05/05/multiline-toast-message-lightning-component/
-
1You cannot use it in LWC - cannot modify elements from shadow DOM Commented Sep 18, 2019 at 8:10
-
I am facing the same issue as I am using LWC here. @David can u suggest a way to make it work in LWC. Commented Sep 18, 2019 at 10:51