I am getting value as undefined when I try to access this.perMonth from fnTwo() and fnThree() but works in fnOne(). I can run a function from data(){} and can return some values but cannot return that's in data(){} eg.this.perMonth (check fnThree())
Vue.component('BuySubscription', {
template: '#buy-subscription',
data() {
return {
perMonth: 19,
valFromFnTwo: this.fnTwo(),
valFromFnThree: this.fnThree()
}
},
methods: {
fnOne() {
console.log("from fnOne: get data > perMonth: " + this.perMonth);
return this.perMonth
},
fnTwo() {
console.log("from fnTwo: get data > perMonth : " + this.perMonth);
return this.perMonth
},
fnThree() {
console.log("from fnThree: get data > perMonth " + this.perMonth);
console.log("from fnThree: get data > valFromFnTwo: " + this.valFromFnTwo);
return 123 // retruns static value
}
}
});
new Vue({
el: '#app',
});
body { font-family: arial; font-size: 12px}
p {margin: 0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div id="app" class="col-lg-6 d-flex align-items-center">
<buy-subscription></buy-subscription>
</div>
<script type="text/x-template" id="buy-subscription">
<div>
<p>value from data > perMonth: {{perMonth}}</p>
<p>value from data > valFromFnTwo: {{valFromFnTwo}} <span style="color: red"> <-- getting Undefined here (see console)</span></p>
<p>value from fnOne(): {{fnOne()}}</p>
<p>value from fnTwo(): {{fnTwo()}}</p>
<p>value from fnThree(): {{fnThree()}}</p>
</div>
</script>
Also, please consider if I have nested array of data which I like to process:
data() {
return {
perMonth: 19,
someVarViaFns: [
{
valFromFnTwo: this.fnTwo(1),
valFromFnThree: this.fnThree(2)
},
{
valFromFnTwo: this.fnTwo(5),
valFromFnThree: this.fnThree(9)
},
]
}
}