๐ฟ Vue.js Tutorial โ The Complete Guide for Beginners & Developers
๐ What is Vue.js?
Vue.js is a progressive JavaScript framework for building user interfaces and single-page applications. Itโs lightweight, flexible, and beginner-friendly, making it one of the most popular front-end frameworks today.
โ
Open-source
โ
Component-based
โ
Easy to integrate
โ
Used by Alibaba, Xiaomi, GitLab, and more
๐ Official Website โ Vue.js
โ๏ธ Installing Vue.js
๐ฅ Option 1: CDN (Quick Start)
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
๐ฅ๏ธ Option 2: Vue CLI
npm install -g @vue/cli
vue create my-project
cd my-project
npm run serve
๐ Vue CLI Guide
๐ Understanding Vue Instance
const app = Vue.createApp({
data() {
return {
message: 'Hello Vue!'
};
}
});
app.mount('#app');
Each Vue app begins by creating a Vue instance using Vue.createApp()
.
๐ Template Syntax
Vue uses an HTML-based template syntax:
<p>{{ message }}</p>
You can also bind raw HTML:
<p v-html="rawHTML"></p>
๐๏ธ Data Binding
๐งฉ One-Way Binding
<p>{{ username }}</p>
๐ Two-Way Binding
<input v-model="username" />
This creates real-time sync between data and UI.
๐ง Vue Directives
Vue comes with special directives for DOM interaction:
Directive | Usage |
---|---|
v-if | Conditional rendering |
v-for | Looping through arrays |
v-model | Two-way data binding |
v-bind | Bind attribute values dynamically |
v-on | Handle events |
๐ Event Handling
<button v-on:click="count++">Clicked {{ count }} times</button>
Or shorthand:
<button @click="increment">Click Me</button>
methods: {
increment() {
this.count++;
}
}
๐ฆ Computed Properties and Watchers
๐งฎ Computed Property
computed: {
fullName() {
return this.first + ' ' + this.last;
}
}
๐ Watcher
watch: {
searchQuery(newVal, oldVal) {
this.fetchResults(newVal);
}
}
๐ฆ Components in Vue
Components are reusable pieces of UI.
app.component('hello-component', {
template: `<h2>Hello from component!</h2>`
});
Use it like this:
<hello-component></hello-component>
๐งฉ Vue Router Basics
For Single Page Apps (SPA):
npm install vue-router
Define routes:
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
];
Create router instance:
const router = createRouter({
history: createWebHistory(),
routes
});
๐ Vuex โ State Management
npm install vuex@next
Create a store:
const store = createStore({
state() {
return { count: 0 };
},
mutations: {
increment(state) {
state.count++;
}
}
});
Use in component:
<button @click="store.commit('increment')">+</button>
๐ Vuex Docs
๐จ Styling in Vue
Scoped styling prevents global CSS leaks:
<style scoped>
h1 {
color: green;
}
</style>
You can also use frameworks like Tailwind CSS, BootstrapVue, or Vuetify.
๐งช Vue DevTools & Debugging
๐งฐ Vue DevTools
Install from Chrome Web Store
Use to:
- Inspect component tree
- Track reactive data
- Debug state management
๐ Best Practices
โ
Split UI into components
โ
Use props and emits wisely
โ
Keep logic separate from template
โ
Always use v-bind:key
in loops
โ
Follow Vue style guide: https://vuejs.org/style-guide/
๐ Conclusion
Vue.js is a game-changer for building modern web interfaces. Whether you’re crafting a small widget or a full-fledged SPA, Vue provides clarity, flexibility, and developer happiness. ๐ฑ
Try building a todo app, then progress to router and Vuexโitโs the perfect progression path for new frontend devs.
โ FAQs
Q1: Is Vue.js good for beginners?
โ
Yes! Vue is intuitive, flexible, and easy to integrate with projects.
Q2: What’s the difference between Vue 2 and Vue 3?
๐ Vue 3 offers better performance, Composition API, and smaller bundle size.
Q3: Can I use Vue with TypeScript?
Yes! Vue 3 has full TypeScript support out-of-the-box.
Q4: Whatโs the best UI library for Vue?
๐ผ๏ธ Try Vuetify, Element Plus, or Quasar.
Q5: Is Vue.js SEO-friendly?
With server-side rendering (Nuxt.js), Vue becomes SEO-optimized.
Share Now :