官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 遵义县| 宁晋县| 阿巴嘎旗| 梁山县| 昌图县| 从江县| 荥阳市| 盈江县| 左权县| 南岸区| 泰宁县| 洛南县| 泰来县| 崇阳县| 汉源县| 蓬安县| 丹东市| 曲周县| 望城县| 天全县| 太白县| 大兴区| 南安市| 沂水县| 桓台县| 北安市| 龙井市| 磐安县| 新建县| 商丘市| 金寨县| 沁水县| 调兵山市| 治多县| 西畴县| 萍乡市| 杭锦旗| 库尔勒市| 罗源县| 澳门| 临泽县|