What is the best way to conditionally apply a class?

后端 未结 22 2781
感动是毒
感动是毒 2020-11-22 09:12

Lets say you have an array that is rendered in a ul with an li for each element and a property on the controller called selectedIndex.

22条回答
  •  面向向阳花
    2020-11-22 09:35

    You can use this npm package. It handles everything and has options for static and conditional classes based on a variable or a function.

    // Support for string arguments
    getClassNames('class1', 'class2');
    
    // support for Object
    getClassNames({class1: true, class2 : false});
    
    // support for all type of data
    getClassNames('class1', 'class2', ['class3', 'class4'], { 
        class5 : function() { return false; },
        class6 : function() { return true; }
    });
    
    

提交回复
热议问题