Vue.js Simplified: Exploring Declarative Rendering and Reactive Data

Vue.js Simplified: Exploring Declarative Rendering and Reactive Data

What is an SFC?

  • SFC stands for Single-File Component.

  • It’s a .vue file that contains HTML, JavaScript, and CSS all in one place.

  • Think of it as a reusable block of code for building parts of a webpage.

#SFC #VueComponents #WebDevelopment

Declarative Rendering

  • Declarative Rendering means you tell Vue what you want to display, and it automatically updates the webpage when the data changes.

  • Example: If a number increases, Vue will automatically show the new number on the page.

#DeclarativeRendering #VueFeatures

Reactive State

  • Reactive State is data that Vue automatically tracks. When this data changes, the webpage updates instantly.

  • Vue provides two ways to create reactive data:

    1. reactive(): Used for objects (like { count: 0 }).

    2. ref(): Used for single values (like numbers or strings).

Example:

import { reactive, ref } from 'vue';

// For objects
const counter = reactive({
  count: 0
});

// For single values
const message = ref('Hello World!');
  • Use counter.count or message.value to access or update the data.

#ReactiveState #VueReactivity

Using Reactive Data in Templates

  • You can display reactive data in your HTML using {{ }} (called "mustaches").

  • Example:

<h1>{{ message }}</h1>
<p>Count is: {{ counter.count }}</p>
  • Vue automatically updates the text when message or counter.count changes.

#VueTemplates #MustacheSyntax

JavaScript in Templates

  • You can use any JavaScript expression inside {{ }}.

  • Example:

<h1>{{ message.split('').reverse().join('') }}</h1>
  • This will reverse the text in message and display it.

#JavaScriptInTemplates #VueFlexibility

Try It Yourself

  1. Create some reactive data using reactive() or ref().

  2. Use it in your template with {{ }}.

  3. See how the webpage updates automatically when the data changes!

#LearnVue #HandsOnCoding

Summary

  • SFC: A .vue file that contains HTML, JavaScript, and CSS.

  • Declarative Rendering: Tell Vue what to display, and it updates the page automatically.

  • Reactive State: Data that Vue tracks and updates in real-time.

  • Use {{ }} to display reactive data in your HTML.

#VueJS #FrontendDevelopment #Web