Work & Development · Frontend Frameworks & TypeScript
Free preview
Set up a Vue project with single-file components
Lesson 1 of Vue: An Alternative Frontend. Anyone can read this lesson with no account. Every lesson after this one is for students.
Templates and components
Create a Vue project and connect a labeled input to a reactive value.
Your goal: Create a Vue project and connect a labeled input to a reactive value.
A different interface model
Complete Build Applications with React first. Vue is another way to describe a screen using data. A template describes the HTML Vue should show. Reactive data is data Vue watches: when it changes, Vue updates the parts of the screen that use it. A single-file component keeps a component's script, template and styles in one .vue file. A ref holds a reactive value. In script code use .value; in the template Vue unwraps it for you. You will build a finder that searches learning resources and keeps a shortlist.
Set up the project
New Vue projects usually start with npm create vue@latest, the official scaffolding tool, which creates the starting project files. It asks a series of questions and adds optional tools such as TypeScript, a router and test setups. So that every lesson behaves the same on every computer, this course builds the same core structure by hand with exact versions. Use Node 22.12 or newer. Create a folder called vue-resource-lab, open a terminal in it and run:
npm init -y
npm pkg set type=module
npm install --save-exact vue@3.5.43
npm install --save-dev --save-exact vite@8.3.3 @vitejs/plugin-vue@6.0.9
mkdir src
@vitejs/plugin-vue teaches Vite to compile .vue files. Create four files.
vite.config.js:
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [vue()],
});
index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Resource finder</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
src/main.js creates the app and mounts it on <div id="app">:
import { createApp } from 'vue';
import App from './App.vue';
createApp(App).mount('#app');
src/App.vue, the first component:
<script setup>
import { ref } from 'vue';
const heading = 'My learning resources';
const topic = ref('HTML');
function clearTopic() {
topic.value = '';
}
</script>
<template>
<main>
<h1>{{ heading }}</h1>
<label for="topic">Learning topic</label>
<input id="topic" v-model="topic">
<p>Current topic: {{ topic }}</p>
<button type="button" @click="clearTopic">Clear topic</button>
</main>
</template>
Start the development server with npx vite --host 127.0.0.1 and open the address it prints.
What you should see
The page shows "My learning resources", an input containing HTML and the line Current topic: HTML. Type CSS: the line changes as you type. Press Clear topic: the input and the line both empty.
<script setup>runs once when the component is created. Everything declared there is available in the template.headingis a plain constant.topicis a ref, so Vue re-renders whatever uses it when it changes.v-model="topic"connects the input to the ref in both directions.- The template writes
{{ topic }}, butclearTopicwritestopic.value, because in script code you work with the ref object itself.
Compared with React: a ref is like a piece of state from useState, but you change it by assigning to .value instead of calling a setter, and Vue tracks the template parts that read it so it can update the affected display.
Practice
- Add a second ref,
goal, with its own labeled input and a line that displays it. - Change Clear topic into a Reset button that restores both starting values.
- Write a table of three inputs and the display you expect, then check each.
Debug
Change topic.value = '' to topic = ''. The development server shows an error overlay about re-assigning the constant topic. Restore it: you change what the ref holds, never the ref itself. If the page stays blank, open the browser console and check that main.js imports ./App.vue with the right spelling.
Check yourself
- Why does the template not need
.value? - Is every variable in
<script setup>reactive? - Does
{{ topic }}turn typed HTML into markup?
Answers
- Vue unwraps refs automatically in templates.
- No. Only values created with
ref,reactiveorcomputedare tracked. - No. Text interpolation escapes it. Avoid
v-htmlwith content you do not control.
Create a README with the finder's audience, the install and start commands, and three fields every resource needs. Next, you will derive search results with computed.
Continue with the full course
This free preview is lesson 1 only. Enroll to open the rest of Vue: An Alternative Frontend.