Comparing methods, watchers, and computed properties
Methods are best used as a handler to an event occurring in the DOM, and in situations where you need to call a function or perform an API call, for example, Date.now()
. All values returned by methods are not cached.
For example, you can compose an action denoted by @click
, and reference a method:
<template> Â Â Â Â <button @click="getDate">Click me</button> </template> <script> export default { Â Â Â Â methods: { Â Â Â Â Â Â Â Â getDate() { Â Â Â Â Â Â Â Â Â Â Â Â alert(Date.now()) Â Â Â Â Â Â Â Â } Â Â Â Â } } </script>
This code block will display an alert bar with the current Unix epoch time whenever a user clicks on the Click me button. Methods should not be used to display computed data, since the return value of...