Vue.js is a progressive JavaScript framework for building reactive UIs. It combines a component-based architecture with a declarative template syntax, enabling DOM updates through its reactivity system. Vue enables the creation of interactive interfaces, ranging from simple widgets to complex single-page applications.

Getting Started with Vue.js

Vue.js supports a CDN setup for rapid prototyping and a project scaffold for structured development. The CDN method runs in-browser with minimal configuration, while the full setup uses the Vue CLI, file-based components, and a module bundler. Both modes initialize applications through the Vue runtime and component system.

CDN (For Quick Prototyping)

code
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
code
<div id="app">{{ message }}</div>
code
<script>
code
const { createApp } = Vue;
code
createApp({ data() { return { message: "Hello Vue!" }; }}).mount('#app');
code
</script>

Explanation:

  • Declares a container element with id="app".
  • {{ message }} is a Vue template interpolation, dynamically binding to the message property in the component's data() object.
  • Destructures createApp from the global Vue object.
  • createApp initializes a new Vue application instance.
code
npm create vue@latest
code
cd your-project
code
npm install
code
npm run dev
code
Key Files:
code
src/App.vue: Root component.
code
src/components/: Reusable components.

Explanation:

  • Runs the official Vue project scaffolding tool (create-vue).
  • Installs all project dependencies listed in package.json.
  • Sets up local node_modules/ for runtime libraries, dev tools, and build plugins.

Vue.js Fundamentals

Vue.js provides a reactive component-based architecture for building user interfaces. It uses a virtual DOM, declarative rendering, and a unidirectional data flow within components. Vue.js includes templates, reactive state, computed properties, directives, and lifecycle hooks. Vue handles state changes through a reactivity system that tracks dependencies and updates the DOM incrementally.

Reactivity System

Vue.js implements a reactivity system that tracks dependencies and updates the DOM when reactive state changes. Vue 3 uses ES6 Proxy to intercept property access and mutation, while Vue 2 relies on Object.defineProperty.

code
const app = Vue.createApp({
code
data() {
code
return {
code
message: 'Hello Vue!'
code
};
code
}
code
});

Explanation:

  • data(): Declares reactive state.
  • Proxy-based reactivity: Automatically tracks nested changes.

Component Structure

Vue components encapsulate template, logic, and style within a single .vue file. Each component defines its structure in the <template>, its behavior in the <script>, and optional styling in the <style>.

code
<template>
code
<button @click="increment">{{ count }}</button>
code
</template>
code

code
<script>
code
export default {
code
data() {
code
return { count: 0 };
code
},
code
methods: {
code
increment() {
code
this.count++;
code
}
code
}
code
};
code
</script>

Explanation:

  • <template>: UI structure with directives.
  • methods: Updates reactive state.

Template Syntax

Vue templates use declarative markup to bind data and render dynamic content. The syntax includes directives such as v-model, v-for, and :key to manage input state, iteration, and DOM diffing. Templates are compiled into render functions that react to data changes and update the DOM incrementally.

code
<template>
code
<input v-model="inputText" />
code
<ul>
code
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
code
</ul>
code
</template>

Explanation:

  • v-model: Two-way input binding.
  • v-for: Renders lists dynamically.

Video Workflow with Vue.js

Vue.js provides reactive bindings and DOM access for controlling video elements. Its component system supports modular UI design for media interfaces. Video playback, state tracking, and event binding use native HTML5 APIs within Vue components.

Custom Video Players

Vue builds custom video players using reactive state and template references. Playback, UI state, and media events stay in sync through component methods. The setup links video element behavior to application state for controlling play, pause, and time updates.

code
<template>
code
<video ref="videoPlayer" @timeupdate="updateTime"></video>
code
<button @click="togglePlay">{{ isPlaying ? 'Pause' : 'Play' }}</button>
code
</template>
code

code
<script>
code
export default {
code
data() {
code
return { isPlaying: false, currentTime: 0 };
code
},
code
methods: {
code
togglePlay() {
code
const player = this.$refs.videoPlayer;
code
this.isPlaying ? player.pause() : player.play();
code
this.isPlaying = !this.isPlaying;
code
},
code
updateTime() {
code
this.currentTime = this.$refs.videoPlayer.currentTime;
code
}
code
}
code
};
code
</script>

Explanation:

  • ref="videoPlayer": Direct access to <video>.
  • @timeupdate: Updates currentTime.

Reactive Video Controls

Reactive video controls use computed properties to track playback state without manual updates. Vue evaluates expressions like progress in response to changes in video time or duration. This enables synchronized UI elements based on real-time media state.

code
<script>
code
export default {
code
computed: {
code
progress() {
code
return (this.currentTime / this.$refs.videoPlayer.duration) * 100;
code
}
code
}
code
};
code
</script>

Explanation:

  • computed: Auto-updates when dependencies change.

Playback State Management

Playback state management uses a centralized store to track video-related data across components. Pinia provides a reactive state container that stores values like volume or track info and exposes actions to update them.

code
import { defineStore } from 'pinia';
code

code
export const usePlayerStore = defineStore('player', {
code
state: () => ({
code
volume: 50,
code
currentTrack: null
code
}),
code
actions: {
code
setVolume(val) {
code
this.volume = val;
code
}
code
}
code
});
code

Explanation:

  • Pinia: Central store for volume, track, etc.

Integration with Video APIs

Vue components can encapsulate external video libraries to handle streaming protocols and playback behavior. Libraries like HLS.js manage adaptive streaming and attach to media elements through lifecycle hooks. Integration occurs during the mounted phase to ensure DOM availability.

code
<script>
code
import Hls from 'hls.js';
code

code
export default {
code
mounted() {
code
const hls = new Hls();
code
hls.loadSource('https://stream.mux.com/example.m3u8');
code
hls.attachMedia(this.$refs.videoPlayer);
code
}
code
};
code
</script>

Explanation:

  • mounted(): Initializes external players post-render.

Performance Optimization

The v-memo directive caches rendered output based on specified reactive values. Vue skips virtual DOM diffing for the block if those values do not change. This reduces re-renders for static or rarely updated sections.

code
<template>
code
<div v-memo="[video.id]">
code
<h3>{{ video.title }}</h3>
code
<p>{{ video.description }}</p>
code
</div>
code
</template>

Explanation:

  • v-memo: Avoids re-render unless key changes.