A
A
andrei_pro2020-01-15 21:42:26
JavaScript
andrei_pro, 2020-01-15 21:42:26

How to pass an instance of a class to components?

Hello.
Structure like this:
Container.js

export default class Container {
 constructor(container) {
    this.container = container
    this.scale = 1
 }

 setScale(scale) {
    this.scale = scale
 }

  cumulative() {
     return Math.PI
  }

  getCumulativeScale() {
      return this.cumulative() * this.scale
  }
}

-Wrapper.vue
--Container.vue
---Scale.vue
----ScaleWrapper.vue
---Element.vue
Container.vue
<template>
  <div>
   Container
  </div>
</template>

<script>
 import Container from './Container'

 export default {
   mounted() {
      const container = new Container(this.$el)
     
     this.$el.addEventListener('mousedown', (e) => {
         let scale = 1//

         container.setScale(scale)
     })
  },
  methods: {}
 }
</script>

In the container variable, an instance of the Container class.
Next, I need to use this container in all components (for example, take getCumulativeScale()) , how to forward it (without mixins and static methods)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alex, 2020-01-15
@Kozack

Perhaps a plugin will suit you: https://ru.vuejs.org/v2/guide/plugins.html
Or here: https://ru.vuejs.org/v2/api/#provide-inject

L
Lev Zabudkin, 2020-01-16
@zabudkin

export const container = new Container(this.$el)

O
OniVe, 2020-01-16
@OniVe

To use shared/inherited components, mixins must be used.
- Using mixins
- Using decorators

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question