- 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.
推薦閱讀
- RCNP實驗指南:構建高級的路由互聯網絡(BARI)
- Twilio Cookbook
- Proxmox High Availability
- Building RESTful Web Services with Spring 5(Second Edition)
- 數字通信同步技術的MATLAB與FPGA實現:Altera/Verilog版(第2版)
- 電力物聯網工程技術原理與應用
- 計算機網絡技術及應用
- Learning Windows 8 Game Development
- 一本書讀懂TCP/IP
- Building RESTful Web Services with .NET Core
- SRv6網絡部署指南
- Guide to NoSQL with Azure Cosmos DB
- 智能物聯網:區塊鏈與霧計算融合應用詳解
- 視聽變革:廣電的新媒體戰略
- Telerik WPF Controls Tutorial