Vue.js course ยท Module 15: TypeScript & Nuxt

Main Project: Typed NOVA LAB Station Panel

2 min read ยท
In this lesson6

Time to combine all the knowledge from this module! We'll create a typed control panel for the NOVA LAB space station that uses TypeScript with Vue 3 and incorporates SSR concepts.

Project Requirements

Our control panel should include:

  1. Typed props - each component declares what data it accepts
  2. Typed emits - events are precisely defined
  3. Typed ref and reactive - the application state is fully typed
  4. Template refs - for direct DOM interaction
  5. Provide/Inject - for passing station configuration

Application Architecture

// types.ts - central application types
interface CrewMember {
  id: number
  name: string
  role: 'commander' | 'engineer' | 'scientist' | 'pilot'
  active: boolean
}

interface StationModule {
  id: string
  name: string
  status: 'online' | 'offline' | 'maintenance'
  power: number
}

interface StationState {
  name: string
  crew: CrewMember[]
  modules: StationModule[]
  alerts: Alert[]
}

interface Alert {
  id: number
  level: 'info' | 'warning' | 'critical'
  message: string
  timestamp: Date
}

ModuleCard Component with Types

<script setup lang="ts">
interface ModuleCardProps {
  module: StationModule
  isSelected?: boolean
}

const props = withDefaults(defineProps<ModuleCardProps>(), {
  isSelected: false
})

const emit = defineEmits<{
  select: [moduleId: string]
  togglePower: [moduleId: string, newStatus: boolean]
}>()
</script>

Panel with Provide/Inject

<script setup lang="ts">
import { provide, reactive, type InjectionKey } from 'vue'

interface StationConfig {
  name: string
  sector: string
  year: number
}

const StationKey: InjectionKey<StationConfig> = Symbol('Station')

provide(StationKey, {
  name: 'NOVA LAB',
  sector: 'Alpha-7',
  year: 2087
})
</script>

SSR Considerations

When building an application with SSR in mind (e.g., with Nuxt), remember:

// Do NOT use browser objects outside of onMounted
import { onMounted, ref } from 'vue'

const windowWidth = ref(0)

// GOOD - window is only available on the client
onMounted(() => {
  windowWidth.value = window.innerWidth
})

// BAD - window doesn't exist on the server!
// const width = window.innerWidth  // ERROR in SSR

An SSR-friendly composable:

function useScreenSize() {
  const width = ref(0)
  const height = ref(0)

  onMounted(() => {
    width.value = window.innerWidth
    height.value = window.innerHeight

    window.addEventListener('resize', () => {
      width.value = window.innerWidth
      height.value = window.innerHeight
    })
  })

  return { width, height }
}

Full Example

Below is an interactive editor with a typed Vue application. Analyze the code and experiment with types - add new properties, change types, and observe how TypeScript helps catch errors.

Code for this lesson: App.vue
<script setup lang="ts">
import { ref, reactive, computed, provide, type InjectionKey } from 'vue'
import ModuleList from './ModuleList.vue'
import AlertPanel from './AlertPanel.vue'

// ===== Types =====
interface CrewMember {
  id: number
  name: string
  role: 'commander' | 'engineer' | 'scientist' | 'pilot'
  active: boolean
}

interface StationModule {
  id: string
  name: string
  status: 'online' | 'offline' | 'maintenance'
  power: number
}

interface Alert {
  id: number
  level: 'info' | 'warning' | 'critical'
  message: string
}

interface StationConfig {
  name: string
  sector: string
  year: number
}

// ===== Provide =====
const StationKey: InjectionKey<StationConfig> = Symbol('Station')
provide(StationKey, { name: 'NOVA LAB', sector: 'Alpha-7', year: 2087 })

// ===== State =====
const modules = ref<StationModule[]>([
  { id: 'm1', name: 'Life Support', status: 'online', power: 95 },
  { id: 'm2', name: 'Navigation', status: 'online', power: 88 },
  { id: 'm3', name: 'Science Lab', status: 'maintenance', power: 40 },
  { id: 'm4', name: 'Communications', status: 'offline', power: 0 },
  { id: 'm5', name: 'Power Grid', status: 'online', power: 72 }
])

const alerts = ref<Alert[]>([
  { id: 1, level: 'info', message: 'System started' },
  { id: 2, level: 'warning', message: 'Module m3 in maintenance mode' }
])

let alertId = 3

// ===== Computed =====
const onlineCount = computed(() => modules.value.filter(m => m.status === 'online').length)
const avgPower = computed(() => {
  const online = modules.value.filter(m => m.status === 'online')
  if (online.length === 0) return 0
  return Math.round(online.reduce((s, m) => s + m.power, 0) / online.length)
})

// ===== Methods =====
function handleToggle(moduleId: string) {
  const mod = modules.value.find(m => m.id === moduleId)
  if (!mod) return
  mod.status = mod.status === 'online' ? 'offline' : 'online'
  mod.power = mod.status === 'online' ? 80 : 0
  alerts.value.push({
    id: alertId++,
    level: mod.status === 'online' ? 'info' : 'warning',
    message: mod.name + ' ' + (mod.status === 'online' ? 'enabled' : 'disabled')
  })
}

function dismissAlert(id: number) {
  alerts.value = alerts.value.filter(a => a.id !== id)
}
</script>

<template>
  <div class="nova-lab">
    <header class="header">
      <h1>NOVA LAB - Typed Panel</h1>
      <div class="stats">
        <span class="stat">Online: {{ onlineCount }}/{{ modules.length }}</span>
        <span class="stat">Avg Power: {{ avgPower }}%</span>
      </div>
    </header>

    <AlertPanel :alerts="alerts" @dismiss="dismissAlert" />
    <ModuleList :modules="modules" @toggle="handleToggle" />
  </div>
</template>

<style scoped>
.nova-lab {
  background: #0a0e27;
  color: #00ff88;
  min-height: 100vh;
  padding: 20px;
  font-family: monospace;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  border-bottom: 1px solid rgba(0,180,216,0.3);
  padding-bottom: 12px;
}
h1 { color: #00ff88; margin: 0; font-size: 18px; }
.stats { display: flex; gap: 15px; }
.stat {
  background: rgba(0,180,216,0.1);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  color: #00b4d8;
}
</style>

Docs entries used in this lesson

Spotted a mistake in this lesson?

  1. Project to submitSubmit Project
  2. VideoModule Summary

Useful articles