Vue.js course Β· Module 1: Vue and Vite
Diagnostic Tools
In this lesson5
In space, diagnostics are a matter of life and death. Vue DevTools is our system scanner.
A panel shows a wrong reading and you don't know why: is it the data, the template, or maybe the component never rendered at all? Adding console.log in ten places is guesswork. You need tools that show you the inside of a running application.
Vue DevTools
Vue DevTools are the official developer tools for Vue; versions 7 and later support only Vue 3. The first way to install them is the browser extension:
- Chrome: Vue.js devtools
- Firefox: Vue.js devtools
- Edge: Vue.js devtools
The second way is the vite-plugin-vue-devtools plugin, which the create-vue wizard adds to the Vite configuration for you. In development mode you then open the panel with a button at the bottom of the page, without installing anything in the browser. In Vite projects I recommend exactly this way - the DevTools documentation recommends it because of its additional features.
Diagnostic Features
- Component Inspector - scan the module tree
- State Inspector - monitor reactive data in real time
- Timeline - track event and change history
- Router - debug navigation
- Pinia - manage the mission database
In current versions, the component inspector and the state inspector form one Components tab: the tree on the left, and on the right the data of the selected component, which you can edit live. The tree of a typical application looks like this:
App
βββ MainLayout (layout: header, sidebar)
βββ DashboardView (view)
βββ OxygenCard (UI)
βββ AlertButton (UI)At the top stands the root component, below it the layout, inside the layout a view, and at the end the smallest interface elements. Click any node and you'll see its data next to it.
Debugging Protocols
Logging system changes
Sometimes you want to react to every change, for example log it to the console or raise an alarm. That's what the watch function is for: the first argument is the watched source, the second is a function that Vue calls after every change, with the new and the old value:
import { ref, watch } from 'vue'
const oxygenLevel = ref(98.5)
// Change monitoring
watch(oxygenLevel, (newLevel, oldLevel) => {
console.log(`Oxygen level: ${oldLevel}% β ${newLevel}%`)
if (newLevel < 90) {
console.error('ALERT: Low oxygen level!')
}
})Set oxygenLevel.value to 88, and the console shows an entry with an arrow followed by a red alert. watch doesn't display anything and doesn't change the oxygen level - it only reacts. The difference from computed is simple: computed calculates a value, watch performs an action.
Debug in template
The second technique is a diagnostic panel in the template. The v-if directive displays an element only when the condition is true, and JSON.stringify turns an object with data into readable text:
<template>
<!-- Diagnostic panel -->
<div v-if="debugMode" class="debug-panel">
<pre>{{ JSON.stringify(systemData, null, 2) }}</pre>
</div>
<!-- Critical indicators -->
<div class="critical-systems">
<span>O2: {{ oxygen }}%</span>
<span>Temp: {{ temperature }}Β°C</span>
<span>Pressure: {{ pressure }} hPa</span>
</div>
</template>When debugMode is false, Vue doesn't create the diagnostic panel at all, and the indicators below it keep working unchanged. The variables used in the template are declared as ref() in the <script setup> section omitted here.
Sensor simulator
For testing, a field in which you type a reading comes in handy. The v-model directive binds the field to a container in both directions: the number you type goes into oxygen, and a change of oxygen in the code goes into the field. The v-if and v-else pair displays one of two paragraphs:
<template>
<input v-model="oxygen" type="number" />
<p v-if="oxygen >= 90">Oxygen normal</p>
<p v-else>ALERT: low oxygen level</p>
</template>
<script setup>
import { ref } from 'vue'
const oxygen = ref(95)
</script>Type 85, and the alert replaces the normal message. v-else must come right after the element with v-if, because it refers to its condition. You'll learn conditional directives in the Hologram Workshop and forms with v-model in the Control Simulator.
Hot Module Replacement
Vite automatically updates modules without restarting, and the server terminal reports which file it swapped:
12:04:31 [vite] (client) hmr update /src/components/LifeSupport.vueThe time format depends on your system settings, and (client) stands for the browser environment. Changes are visible instantly - critical during system development!
Operational Commands
| Command | Description |
|---|---|
npm run dev | Development mode (simulator) |
npm run build | Compile for deployment |
npm run preview | Test production version |
npm run test:unit | Run unit tests (Vitest) |
npm run lint | Check code quality |
These are scripts from package.json that the wizard adds depending on the options you chose - hence test:unit instead of plain test. A typical work cycle: npm run dev, editing components, a refresh through HMR, and before deployment npm run build followed by checking the result with npm run preview.
Preparing for Deployment
The build runs in four stages. Vite starts from index.html and finds the entry script in it. Then Rolldown loads the modules one by one: each is transformed first (the Vue plugin compiles .vue files, and the Oxc tool turns TypeScript and JSX into JavaScript), and only then does Rolldown read its imports and fetch the next files, building a dependency graph. Next, Rolldown bundles the modules into chunks, removes unused code and minifies the result, and finally the files land in the dist folder. Up to version 7 the transformation was done by esbuild and the bundling by Rollup.
# Compile the system
npm run build
# Output in /dist directory
dist/
βββ index.html
βββ assets/
β βββ index-[hash].js # Compiled logic
β βββ index-[hash].css # Compiled styles[hash] is a fingerprint of the file's contents: when the code changes, the name changes too, so the browser won't use an old version from its cache. npm run preview serves this folder locally, but it isn't a production server - we'll deal with deployment at the Launch Platform.
This code will fly to Mars. Every bug could cost the crew's lives. Test thoroughly! In the editor below, change the counter, type a message and toggle the system while watching the same changes in DevTools.
Remember: DevTools shows the inside of the system, watch reacts to every change, and npm run build packs the tested code into the dist folder.
Code for this lesson: src/App.vue
<template>
<div class="nova-lab">
<h1>Vue DevTools</h1>
<section class="info">
<h2>Your best friend during development</h2>
<p>
Vue DevTools is a browser extension that allows you to:
</p>
<ul>
<li>Browse the component tree</li>
<li>Inspect and edit state</li>
<li>Track events</li>
<li>Debug Pinia stores</li>
<li>Analyze performance</li>
</ul>
</section>
<section class="demo">
<h2>Interactive State</h2>
<p>Open DevTools and observe how the state changes:</p>
<div class="state-demo">
<div class="counter">
<button @click="count--">-</button>
<span>{{ count }}</span>
<button @click="count++">+</button>
</div>
<div class="input-demo">
<input v-model="message" placeholder="Type a message..." />
<p>{{ message }}</p>
</div>
<div class="toggle-demo">
<label>
<input type="checkbox" v-model="isActive" />
System active
</label>
<div class="status" :class="{ active: isActive }">
{{ isActive ? 'ONLINE' : 'OFFLINE' }}
</div>
</div>
</div>
</section>
<section class="install">
<h2>Installation</h2>
<div class="browser-links">
<a href="https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd" target="_blank" class="browser-btn">
Chrome
</a>
<a href="https://addons.mozilla.org/en-US/firefox/addon/vue-js-devtools/" target="_blank" class="browser-btn">
Firefox
</a>
<a href="https://microsoftedge.microsoft.com/addons/detail/vuejs-devtools/olofadcdnkkjdfgjcmjaadnlehnnihnl" target="_blank" class="browser-btn">
Edge
</a>
</div>
</section>
</div>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
const message = ref('')
const isActive = ref(true)
</script>
<style>
.nova-lab {
min-height: 100vh;
background: #0a0e27;
color: #e0e0e0;
padding: 2rem;
font-family: system-ui, sans-serif;
}
h1 {
color: #00ff88;
text-align: center;
}
section {
background: rgba(26, 30, 55, 0.8);
border: 1px solid #00b4d8;
border-radius: 12px;
padding: 1.5rem;
margin: 1.5rem auto;
max-width: 600px;
}
h2 {
color: #00b4d8;
margin-top: 0;
}
ul {
list-style: none;
padding: 0;
}
ul li {
padding: 0.5rem 0;
border-bottom: 1px solid rgba(0, 180, 216, 0.2);
}
ul li::before {
content: "Β» ";
color: #00ff88;
}
.state-demo {
display: flex;
flex-direction: column;
gap: 1rem;
}
.counter {
display: flex;
align-items: center;
justify-content: center;
gap: 1rem;
}
.counter button {
width: 40px;
height: 40px;
background: #00b4d8;
color: #0a0e27;
border: none;
border-radius: 8px;
font-size: 1.25rem;
cursor: pointer;
}
.counter span {
font-size: 1.5rem;
font-weight: bold;
color: #00ff88;
min-width: 60px;
text-align: center;
}
.input-demo input {
width: 100%;
padding: 0.75rem;
background: #1a1e37;
border: 2px solid #00b4d8;
border-radius: 8px;
color: #fff;
}
.input-demo p {
color: #00ff88;
font-style: italic;
min-height: 1.5rem;
}
.toggle-demo {
display: flex;
align-items: center;
justify-content: space-between;
}
.toggle-demo label {
display: flex;
align-items: center;
gap: 0.5rem;
cursor: pointer;
}
.status {
padding: 0.5rem 1rem;
border-radius: 20px;
font-weight: bold;
background: rgba(255, 68, 68, 0.2);
color: #ff4444;
}
.status.active {
background: rgba(0, 255, 136, 0.2);
color: #00ff88;
}
.browser-links {
display: flex;
gap: 1rem;
justify-content: center;
flex-wrap: wrap;
}
.browser-btn {
padding: 0.75rem 1.5rem;
background: #1a1e37;
border: 2px solid #00b4d8;
border-radius: 8px;
color: #00b4d8;
text-decoration: none;
transition: all 0.2s;
}
.browser-btn:hover {
background: #00b4d8;
color: #0a0e27;
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What are the official developer tools for Vue.js called?
2. What command compiles a Vue/Vite project for production?
3. What is the watch() function used for in Vue 3?
4. Who is the creator of both Vue.js and Vite?
Hands-on tasks in the game
- Vertical ordering
Arrange the process of installing and running a Vue.js project from scratch:
- Code editor
Complete the component with watch() monitoring changes to a reactive variable and logging them to the console.
- Click in order
Arrange the syntax of the watch function in Vue 3:
- Vertical ordering
Arrange the typical Vite workflow from development to production:
- Horizontal ordering
Arrange the syntax for conditional rendering of an element:
- Code editor
Use v-if and v-else to conditionally display system status depending on a variable's value.
- Vertical ordering
Arrange the Vue project structure from the entry file to the component:
- Horizontal ordering
Arrange the syntax of a button with click event handling in Vue:
- Click in order
Arrange the correct import of the ref function from Vue:
- Vertical ordering
Arrange the order of operations in the main.js file:
- Horizontal ordering
Arrange the syntax for dynamic CSS class binding in Vue:
- Click in order
Arrange the syntax of a computed property in Vue 3:
- Vertical ordering
Arrange the stages of the Vite build process from source code to production:
- Click in order
Arrange the correct style section with the scoped attribute:
- Horizontal ordering
Arrange the syntax of an input with two-way data binding in Vue:
- Vertical ordering
Arrange the component hierarchy from the highest to the lowest level:
Tasks after this lesson
- QuestionDevTools name for Vue
- Put in orderVue.js installation process
- Coding taskAdvanced: Practice: Watch and debugging
- QuestionAdvanced: Build command in Vite
- Click in orderAdvanced: watch syntax in Vue
- Put in orderAdvanced: Vite development workflow
- QuestionAdvanced: The watch() function in Vue
- Put in orderAdvanced: v-if directive syntax
- Coding taskAdvanced: Practice: Conditional rendering
- QuestionAdvanced: Creator of Vue.js and Vite
- Put in orderAdvanced: Vue project structure hierarchy
- Put in orderAdvanced: @click event handling syntax
- Click in orderAdvanced: Importing ref from Vue syntax
- Put in orderAdvanced: Script order in main.js
- Put in orderAdvanced: v-bind attribute syntax
- Click in orderAdvanced: computed property syntax
- Put in orderAdvanced: Vite build stages
- Click in orderAdvanced: style scoped syntax
- Put in orderAdvanced: v-model syntax on input
- Put in orderVue component hierarchy