上QQ阅读APP看书,第一时间看更新
Vue methods
The methods option in a Vue instance just lists all the functions that exist on that Vue instance (or on a Vue component).
The methods option works with the data of the Vue instance. What follows is a simple demonstration of this concept in practice:
// HTML
<div id="definitions">
<!-- 'whatIsVue' and 'whyUseVue' are functions defined in the 'methods' option in the Vue instance -->
<button id="btn" v-on:click="whatIsVue">What is Vue?</button>
<button id="btn" v-on:click="whyUseVue">Why use Vue?</button>
</div>
// JS
var definitions = new Vue({
el: '#definitions',
data: {
name: 'Vue.js'
},
// define methods (functions) under the `methods` object
methods: {
whatIsVue: function () {
console.info(this.name + ' is a Progressive Front-end Framework')
},
whyUseVue: function () {
alert('Because ' + this.name + ' is nice.')
}
}
})
As we can see, the data option holds the Vue.js string, which can be accessed via the name key.
Inside the methods option, we can see two functions: whatIsVue and whyUseVue. The whatIsVue function takes the click event and logs out the value inside name to the console. The whyUseVue function inside the methods option works similarly.
This code can be seen in a pen at this address: https://codepen.io/AjdinImsirovic/pen/yEPXdK.