Vue.js provides a set of directives that allow you to manipulate the DOM and control rendering behavior. Two such directives are `v-pre` and `v-once`. `v-pre` is used to skip compilation of an element and all of its children, while `v-once` is used to render an element and its children only once and prevent subsequent updates. In this guide, we'll explore how to use these directives and provide sample code to demonstrate their usage.

Sample Code

Let's create a Vue.js application that demonstrates the use of `v-pre` and `v-once` directives:

<div id="app">
<div v-pre>
<h2>{{ title }}</h2>
<p>This is a v-pre directive.</p>
<h2 v-once>{{ title }}</h2>
<p v-once>This is a v-once directive.</p>
const app = new Vue({
el: '#app',
data: {
title: 'Vue.js Directives Demo'

In this code:

  • We create a Vue component with two `div` elements, each containing an `h2` element and a `p` element.
  • The first `div` uses the `v-pre` directive, which prevents the compilation of its content and renders it exactly as-is, including the `{{ title }}` expression.
  • The second `div` uses the `v-once` directive on both the `h2` and `p` elements. This causes the elements and their content to be rendered only once and not updated, even if the underlying data changes.
  • We define a Vue instance with a `title` data property that is used in the templates of both `div` elements.