How do directives differ from regular HTML attributes?
Vue directives and regular HTML attributes look similar (both are attributes in markup), but they work in completely different ways. Here is a clear explanation that sounds good in an interview.
The main difference
HTML attributes are static properties of an element.
They simply set a fixed value: id, class, href, disabled, value, etc.
Vue directives are dynamic instructions.
They tell the framework how to update the DOM depending on the state of the data.
Detailed differences
1. Directives are reactive, HTML attributes are not
An HTML attribute sets a value once:
<input value="Hello">If the value changes in JavaScript, the HTML attribute does not update.
A Vue directive reacts to data changes:
<input :value="message">message = 'New value' // the DOM updates automatically2. Directives execute logic, not just set properties
An HTML attribute cannot:
- conditionally show/hide an element
- render lists
- listen to events with a reactive context
- update automatically
Directives can:
<div v-if="isLoggedIn">Welcome!</div>
<button @click="logout">Log out</button>
<li v-for="item in list">{{ item }}</li>These are not just attributes, they are instructions for Vue's template engine.
3. Directives have a lifecycle and DOM interaction logic
Regular HTML attributes are just text.
A custom directive can execute JavaScript:
app.directive('focus', {
mounted(el) {
el.focus();
}
});4. HTML attributes work in plain HTML, directives work only inside Vue
This code is valid in plain HTML:
class="button"
disabledBut directives with v- work only inside a Vue template:
v-if="show"
v-model="value"
v-for="item in list"If you insert such code into plain HTML, it will just be a string with no logic.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.