How to call a function in VUE template?
Here we will define and call a function in vue template. We need to define a function in methods and use a click event to call the function from the template.
Here, we will learn on how to call a function in vue template.
First we have to define the function in the methods. and then we can use an event listener to call the function from the vue template.
In this example we will increment a number using a function and call the function using a click event from the vue template.
Define and call a function in vue template.
First lets create the data and the methods in vue.
In script:
<script> export default { data(){ return{ number: 0 } }, methods:{ increaseNumber(){ this.number ++ } } } </script>
Here i have created a function call increaseNumber(), which will increment the number with + 1 whenever we call the function with a click Event.
In template:
<template> <div> <button @click="increaseNumber()">Increase Number</button> The Number is : {{ number }} </div> </template>
Now whenever we click the function from the template, it will call the function and it will increase the value of the number by + 1.
Related Topics:
Call a Vue.js Component Method From Outside The Component
Related Posts
Easy Way to use localStorage with Vue
Short article on how to use local storage with Vue and learn saving and storing data in local storage with the setItem() and getItem() methods.
Force update Vue to Reload/Rerender component
Short tutorial on how to correctly force update Vue.js component to reload or rerender. The component is reinitialized without having to do a browser refresh.
Set and get cookies in a browser in Vue App
Find out how to set and get cookies in a browser for your webpage in Vue using vue-cookies package.
Save vuex state after page refresh in Vue App
Short tutorial on how to save (or persist) data in a vue application using Vuex and vuex-persist npm package.
