Vue3 v-cloak Directive
v-cloakThe directive is used to hide uncompiled templates until the component instance is ready.
Basic Explanation
v-cloakThe directive is used to solve the "uncompiled template flash" problem when using DOM templates. When users visit the page, they may see the raw{{ }}mustache tags until the Vue component mounts and renders the content.
v-cloakIt remains on the element until the associated component instance is mounted. Combined with CSS rules, it can hide the raw template during this period.
- Expected Type: None (does not accept expressions)
- Purpose: Hides uncompiled templates until the component is ready.
Note: This directive is only needed in setups without a build step.
Basic Usage
When usingv-cloak, you need to combine it with CSS rules:
Example
[v-cloak] {
display: none;
}
Example
{{ message }}
</div>
When compilation is complete,v-cloakthe attribute is automatically removed from the element, and the element is displayed.
Use Cases
v-cloakMainly used in the following scenarios:
1. Using CDN to Import Vue
When importing Vue via CDN without a build step, during the brief period between page load and the Vue script, the template may not yet be compiled:
Example
<html>
<head>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<style>
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<div id="app">
<div v-cloak>
{{ message }}
</div>
</div>
<script>
const { createApp } = Vue
createApp({
data() {
return {
message: 'Hello Vue!'
}
}
}).mount('#app')
</script>
</body>
</html>
2. Complex Template Structures
For complex templates with multiple data bindings, v-cloak prevents users from seeing unrendered content:
Example
<h1>{{ title }}</h1>
<p>Welcome, {{ username }}</p>
<ul>
<li v-for="item in items" :key="item.id">
{{ item.name }}
</li>
</ul>
</div>
How v-cloak Works
- When the page loads, Vue has not yet been initialized, and the
v-cloakattribute exists on the element. - CSS rules
[v-cloak] { display: none; }hide these elements. - Vue initializes and compiles the template, creating reactive data.
- After the component is mounted, Vue automatically removes the
v-cloakattribute. - Elements become visible, displaying the rendered content.
Notes
Only for scenarios without a build step: When using build tools (such as Vite, Webpack), the template is already compiled during the build phase, so v-cloak is not needed.
Example
/* It is recommended to place the CSS in the head */
<head>
<style>
[v-cloak] {
display: none;
}
</style>
</head>
Need to be used with CSS: v-cloak itself does not hide content; it must be combined with CSS rules to take effect.
Differences from Modern Build Tools
When using modern build tools (such as Vite), there is no need for v-cloak because:
- The template is precompiled during the build phase
- The JavaScript is inlined or asynchronously loaded after bundling
- The initial HTML does not contain uncompiled template syntax
Therefore, v-cloak is mainly used in traditional CDN import methods or simple HTML page scenarios.
Other Extensions
Vue3 Built-in Directives