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

68
Views
Using normal classes with vue composition api

how can I use framework-independent JavaScript class instances with reactive state on vue-composition-api.

Given following class:

// CounterService / counter-service.ts
//
// this is a normal javascript class which is independent of vue
export const counterService = new (class CounterService {
  private count: number = 0;

  // should cause all components that use `getCount` to react.
  addOne(): void {
    this.count++;
  }

  // should be reactive
  getCount(): number {
    return this.count;
  }
})()

Kind regards Rozbeh Chiryai Sharahi

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

most important hint: Use reactive or ref on all components involved (those that update and those that read).

// Component A / Counts.vue
<template>
  <div>{{services.counterService.getCount()}}</div>
</template>
<script lang="ts">
import { counterService } from 'counter-service.ts'
export default defineComponent({
  setup() {
    // You could also use ref, but i prefer to avoid the `.value`
    const services = reactive({
      counterService
    })

    return { services }
  }
});
</script>
// Component B / IncrementCountButton.vue
<template>
  <div @click="services.counterService.addOne()">Increment</div>
</template>
<script lang="ts">
import { counterService } from 'counter-service.ts'
export default defineComponent({
  setup() {
    // You could also use ref, but i prefer to avoid the `.value`
    const services = reactive({
      counterService
    })

    return { services }

  }
});
</script>

Kind regards Rozbeh Chiryai Sharahi

about 4 years ago · Juan Pablo Isaza Report
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!