Vue

by spronkets

HTML

<div id="app">
  <h2>Value: <span class="value">{{testComponentValue}}</span></h2>
</div>

Vue

if (!Vue.prototype.$TestService) {
  Vue.prototype.$TestService = new Vue({
    data() {
      return {
        valueTimeout: undefined,
        value: 0
      };
    },
    mounted() {
      this.increaseValue();
    },
    destroyed() {
      clearTimeout(this.valueTimeout);
    },
    methods: {
        increaseValue() {
            this.valueTimeout = setTimeout(() => {
                this.value++;
                this.increaseValue();
            }, 1000);
        }
    }
  }).$mount(); // don't use `el: "#app"`` as a solution...
}

new Vue({
  el: "#app",
  computed: {
    testComponentValue() {
        return this.$TestService.value;
    }
  }
});