how to reuse es6 class in vue js?
It should work with getA()
as a computed property rather than a method. Also, you can skip the if statement as no return statement will return undefined
.
computed: { getA() { return objA.a; }}
You are creating the instance in the global scope. You need to instantiate your object in the data
field for vue to be able to track any changes ..
data: { objA: new A();},
Then you can either use a method like you did ..
methods: { getA() { return this.objA.a; }},
<div>{{getA()}}</div>
Or use a computed property like others have said ..
computed: { getA() { return this.objA.a; }}
<div>{{getA}}</div>
Both will have the same effect, but it's better to use a computed property to take advantage of the caching.