Question presented to candidate: "A status badge needs a red background when there is an error, a bold font when it is active, and a font size that comes from a slider. Show me how you would bind the classes and styles in Vue, and tell me what happens when the badge is a child component and the parent also passes a class."
What a strong answer should cover:
:classaccepts a string, an object ({ active: isActive }, key = class name, value = on or off) or an array that can mix both.- A static
classattribute and a bound:classon the same element are merged, not overwritten. :styleaccepts an object (camelCase or quoted kebab-case keys) or an array of objects that are merged left to right; units are your job.- Put non-trivial logic in a computed object rather than a long inline expression.
- On a component,
classandstylefrom the parent fall through to the single root element and merge with its own; a multi-root component needs$attrs.classbound explicitly, otherwise Vue warns. - Under the hood the compiler wraps the bindings in
normalizeClass/normalizeStyleand marks the element with the CLASS / STYLE patch flags.
Clarifying questions expected:
- "Is the badge a native element or a child component, and does that component have one root element?"
- "Are the colours design tokens (classes) or truly dynamic values (inline styles or CSS variables)?"
Code / implementation expected: Yes. A template using object and array syntax for both bindings, ideally driven by a computed property.