Is there a way to dynamically compute some of the data
variables in a Vue
instance by referencing other variables and tracking their changes?
new Vue({
el: '#root',
data: {
x: 1,
y: x + 1
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>
<div id="root">
{{ x }} and {{ y }}
</div>
This approach results in an error message:
Uncaught ReferenceError: x is not defined
.
How can I utilize previously defined variables to create new ones on-demand?