Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Track typescript Class property in vuejs3 components

I'm making a web game with typescript in Vuejs3 but I'm struggling to make my typescript class properties reactive in my .vue components.

I've simplified the class to make it clearer.

Game.ts:

import { Clock } from "@/modules/Clock";

export class Game {
  public clock: Clock;

  constructor() {
    this.clock = new Clock(60);
  }
}

Clock.ts:

export class Clock {
  private _updateTimeRequest = 0;

  public initialTime: number;
  public timeLeft: number;

  constructor(startTime: number) {
    this.initialTime = startTime;
    this.timeLeft = startTime;

    this.start();
  }

  private updateTime(): void {
    this.timeLeft--;

    if (this.timeLeft <= 0) {
      this.stop();
    }
  }

  public start() {
    this.timeLeft = this.initialTime;

    this._updateTimeRequest = setInterval(this.updateTime.bind(this), 1000);
  }

  public stop() {
    clearInterval(this._updateTimeRequest);
  }
}

GameView.vue:

import GameClock from "@/components/GameClock.vue";

import { Game } from "@/modules/Game";
import { provide, ref } from "vue";

let game = ref(new Game());

provide("game", game);
</script>

<template>
  <div>
    <GameClock />
  </div>
</template>

<style scoped></style>

GameClock.vue:

<script setup lang="ts">
import { computed, inject } from "vue";

const game = inject("game");

const timeLeft = computed(() => {
  return game.value.clock.timeLeft;
});
</script>

<template>
  <div>
    <span>{{ timeLeft }}</span>
  </div>
</template>

<style scoped></style>

So the question is, how do I make my timeLeft property reactive so it can display the countdown of the game.clock instance ?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!