Now we want to bind button click functionality between components so that we can make calculations variable dynamic.
The concepts being introduced here are event handling and event emitters.
Event handling
We can use the v-on directive, which we typically shorten to the @ symbol, to listen to DOM events such as click, mouseover, keyup, etc, and run some JavaScript when they're triggered.
The usage would be v-on:click="handler" or with the shortcut, @click="handler"
Now, let's update each button from the AppButtons component to have a click event that sends a value up the tree using $emit method to the parent component, then updates the calculations variable via a prop from the App.vue
Event emitters
Events are emitted using the $emit method in the template or by defineEmits in script setup. The parent can then listen to it using v-on. Note: We can emit a camelCase event, but can listen for it using a kebab-cased listener in the parent.
In the buttonAction function, we call $emit method for each condition satisfied. For instance, symbols such as tan, cos are added with "(", so need to be rendered as so! The basics such as numerics and basic operators are added directly. I've used regex to match for the symbols.
Then, when we listen to the event in the parent, we can use an inline arrow function as the listener, which allows us to access the event argument.
When we catch the event in the listener, we call a function updateScreen that updates the result variable passed as prop. If the variable is initially undefined, set the value, otherwise, update it.