Answer the question
In order to leave comments, you need to log in
How does reactive work in Composition API classes?
In setup I create something like this:
const abc = reactive(new KakoyToClass())
return {abc}
<template>
<div style="width: 200px; height: 200px">{{player.audioPlayer.isPlaying}}</div>
</template>
setup(props) {
const player = reactive(new AnotherClass())
return {player}
},
private _onPlaying = this.onPlaying.bind(this)
constructor() {
this.audioPlayer = {
active: false,
initialized: false,
element: new Audio(''),
isPlaying: false,
currentPlaying: {
playlist: [],
index: 0
}
}
this.audioPlayer = reactive(this.audioPlayer) // если закомментить, не увидим никаких изменений, даже после того, как в onPlaying выставился true
this.init()
}
private init(){
this.audioPlayer.element.addEventListener('playing', this._onPlaying)
this.audioPlayer.initialized = true
}
private onPlaying() {
this.audioPlayer.isPlaying = true
}
Answer the question
In order to leave comments, you need to log in
answered on stack
reactive creates a proxy to original instance, so when methods are called on reactive proxy object, they receive it as this.
When listeners are set up in a constructor, they are hard-coded to use original non-reactive this.
In order for a class to be unaware of Vue reactive helpers, it should be designed in a way that allows to not use this in a constructor for setting initial data and not side effects. Callbacks shouldn't be bound to this in constructor, this includes class fields because are syntactic sugar for constructor body.
const player
methods can be called through this instance / proxy (in my case), and reactive this or something like that will be passed to these methods. That is: init()
) that will initialize the listeners, and the callbacks of these listeners should be an arrow function. After these manipulations there will be reactivity without Vyushny pieces.Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question