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

  • Vue.js Quick Start Guide
  • Ajdin Imsirovic
  • 185字
  • 2021-06-24 18:23:56

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.

主站蜘蛛池模板: 清徐县| 卫辉市| 兖州市| 衡阳县| 保德县| 十堰市| 永昌县| 太和县| 吴桥县| 抚顺县| 高清| 青龙| 二手房| 高碑店市| 聊城市| 三原县| 九龙县| 社旗县| 丹江口市| 堆龙德庆县| 南宫市| 枞阳县| 海口市| 思茅市| 剑河县| 瑞昌市| 宜城市| 科尔| 麟游县| 义乌市| 双柏县| 彭泽县| 綦江县| 商水县| 新巴尔虎右旗| 延庆县| 宁河县| 黄浦区| 江达县| 新乡县| 定日县|