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

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.

主站蜘蛛池模板: 嘉义市| 惠水县| 洛阳市| 尼勒克县| 南溪县| 峡江县| 江永县| 美姑县| 兴海县| 宿迁市| 青岛市| 长海县| 鲁甸县| 茌平县| 府谷县| 安溪县| 普格县| 石楼县| 宜阳县| 张家口市| 威远县| 高州市| 师宗县| 平乐县| 大田县| 方城县| 无极县| 田林县| 抚州市| 柘荣县| 满洲里市| 晋城| 松江区| 仪陇县| 拜泉县| 商水县| 蒙阴县| 大城县| 西丰县| 浮山县| 关岭|