# Vue.js Introduction for Python Developers A practical guide to Vue.js, written for developers who know Python. --- ## Table of Contents 1. [What is Vue?](#what-is-vue) 2. [The Big Idea: Reactivity](#the-big-idea-reactivity) 3. [Your First Vue Component](#your-first-vue-component) 4. [Reactive State with `ref`](#reactive-state-with-ref) 5. [Derived State with `computed`](#derived-state-with-computed) 6. [Templates: HTML with Vue Features](#templates-html-with-vue-features) 7. [Two-Way Binding with `v-model`](#two-way-binding-with-v-model) 8. [Events and User Interaction](#events-and-user-interaction) 9. [Components and Props](#components-and-props) 10. [Lifecycle Hooks](#lifecycle-hooks) 11. [Composables: Reusable Logic](#composables-reusable-logic) 12. [A Real-World Example: Filter Flow](#a-real-world-example-filter-flow) 13. [Vue Strengths, Weaknesses, and Use Cases](#vue-strengths-weaknesses-and-use-cases) 14. [Quick Reference](#quick-reference) --- ## What is Vue? Vue.js is a JavaScript framework for building user interfaces. It focuses on the **view layer** - what the user sees and interacts with. ### Vue 2 vs Vue 3 This guide covers **Vue 3**, which introduced the Composition API (using ` ``` When `count.value` changes, Vue re-renders the button text. You never call `update_view()`. --- ## Your First Vue Component A Vue **single-file component** (`.vue` file) has three sections: ```vue ``` ### Mental Model When reading Vue code, ask four questions: 1. **What values are reactive state?** → Look for `ref()` and `computed()` 2. **Which template elements use that state?** → Look for `{{ variable }}` and `v-model` 3. **Which functions change that state?** → Look for functions that modify `.value` 4. **Which computed values depend on that state?** → Look for `computed()` --- ## Reactive State with `ref` ### What is `ref`? `ref()` wraps a value so Vue can track changes to it. ```ts import { ref } from 'vue'; const count = ref(0); // reactive number const username = ref('alice'); // reactive string const isLoading = ref(false); // reactive boolean const items = ref([]); // reactive array ``` ### Reading and Writing In ` ``` The button label and disabled state both react to `isLoading`. --- ## Derived State with `computed` Use `computed()` for values that are calculated from other reactive values. ```ts import { ref, computed } from 'vue'; const firstName = ref('Ada'); const lastName = ref('Lovelace'); const fullName = computed(() => { return `${firstName.value} ${lastName.value}`; }); ``` ### Python Comparison This is like a Python `@property`: ```python class Person: @property def full_name(self): return f"{self.first_name} {self.last_name}" ``` ### Rule of Thumb | Use `ref` when | Use `computed` when | |---------------|---------------------| | The value changes over time | The value is calculated from other values | | The value is primary state | You don't want to duplicate state | | Examples: user input, API data | Examples: full name from first + last | ### Common Mistake: Storing What Can Be Derived ```ts // Bad: duplicate state, must update manually const firstName = ref('Ada'); const lastName = ref('Lovelace'); const fullName = ref('Ada Lovelace'); // must keep in sync! // Good: computed handles it automatically const fullName = computed(() => `${firstName.value} ${lastName.value}`); ``` ### Real Example: Validation ```ts const selectedStart = ref(null); const selectedEnd = ref(null); const hasValidRange = computed(() => { if (!selectedStart.value || !selectedEnd.value) { return true; // missing values are valid (no filter) } return new Date(selectedStart.value) <= new Date(selectedEnd.value); }); ``` --- ## Templates: HTML with Vue Features ### Interpolation: `{{ }}` Print values into HTML: ```vue

{{ username }}

{{ 2 + 3 }}

{{ isLoading ? 'Loading...' : 'Done' }}

{{ items.length }} items

``` ### Conditional Rendering: `v-if` / `v-else` Show elements based on conditions: ```vue

{{ error }}

No errors

Welcome, {{ username }}!

Please log in.

``` ### Loops: `v-for` Repeat elements for each item: ```vue
  • {{ user.name }}
``` **Important:** Always use `:key` with `v-for` for proper list rendering. ### Attribute Binding: `:` Bind HTML attributes to JavaScript expressions: ```vue View Profile ``` Short for `v-bind:disabled`. ### Class Binding ```vue
Content
``` --- ## Two-Way Binding with `v-model` `v-model` connects a form field and a reactive variable bidirectionally. ### Basic Input ```vue ``` - User types → `email` updates - Code changes `email` → input display updates ### Select Dropdown ```vue ``` ```ts const countries = [ { title: 'Germany', value: 'DE' }, { title: 'France', value: 'FR' }, { title: 'Spain', value: 'ES' }, ]; const selectedCountry = ref(null); ``` - `item-title`: what's shown to users - `item-value`: what's stored in the variable --- ## Events and User Interaction ### Click Events: `@click` ```vue ``` ```ts function save() { console.log('saving...'); } ``` ### Other Common Events ```vue