Skip to content
Advertisement

Vue – adding a Class-Name to a Component using a prop

I would like to make a custom component in Vue with the following props:

  • text = text to be displayed in the link.
  • icon = identifier of the icon to be displayed next to the link.

< sidebar-link text=”Home” icon=”fa-home”>

I have the following .Vue template for my component.

JavaScript

Essentially I have the default Font-Awesome code in my template:

JavaScript

and I want add a class to my component by using it’s prop.

JavaScript

thanks.

Advertisement

Answer

So when your property icon is holding the value home you can use some class binding like the following (Thank god js expression are allowed within attributes in Vue.js):

JavaScript

or even shorter

JavaScript

This will result in

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement