search

Home  >  Q&A  >  body text

How to modify the color of text using the ternary operator in Vue?

I want to change the text color coming from the endpoint. It only displays the value of the ternary operator ('backgroundColor:green) on the UI. Can anyone help me?

mainTrack() {
 this.axios
    .get(
      `${configObject.apiBaseUrl}/Maintenance/Company`,
      configObject.authConfig()
    )
    .then((res) => {
     this.maintainTrack= res.data;

     this.maintainTrack.forEach(element => {
       element.isResolve = element.isResolve== 'true' ? 'backgroundColor:green' :
        'backgroundColor:red'
            });
    })
    .catch((error) => {});
},

P粉545218185P粉545218185282 days ago377

reply all(1)I'll reply

  • P粉481815897

    P粉4818158972024-03-20 18:14:53

    Try returning a style object instead of a string:

     element.isResolve = { backgroundColor : element.isResolve ? 'green':'red' }
    

    reply
    0
  • Cancelreply