[英]How to call a function after selecting an option from a select element in Vue?
I have a couple of options in a <select>
element.我在<select>
元素中有几个选项。 How can I call a function when I choose an option?选择选项时如何调用函数?
Here is what I have so far in terms of the JS:以下是我迄今为止在 JS 方面的内容:
export default {
name: 'Dropdown',
methods: {
onChange() {
logout();
},
},
computed: {
logout() {
console.log('logout);
},
},
};
My select menu looks like this: <select @change="onChange">...
我的选择菜单如下所示: <select @change="onChange">...
When I select an option, I receive the following error: logout is not defined
.当我选择一个选项时,我收到以下错误: logout is not defined
。 Any ideas?有任何想法吗?
you need to use this to reference the instance, so it will be this.logout();
您需要使用它来引用实例,因此它将是this.logout();
EDIT:编辑:
You need to create an onLogout in the methods property if you want to do something particular, a computed property is a reactive property that return something, it isn't really meant to do something that handle the logout.如果你想做一些特定的事情,你需要在方法属性中创建一个 onLogout ,计算属性是一个返回一些东西的反应属性,它并不是真的要做一些处理注销的事情。
As i see your logout computed property would be more about getting the current status of the user if he's logout or not, but it would be better to rename it.正如我所看到的,您的注销计算属性将更多地用于获取用户的当前状态(如果他是否注销),但最好重命名它。
So if you want to do something particular when the select change(like a logout method)因此,如果您想在选择更改时执行某些特定操作(例如注销方法)
you need to adapt your component this way:您需要以这种方式调整您的组件:
export default {
name: 'Dropdown',
methods: {
onChange() {
this.logout();
},
logout() {
console.log('logout called');
},
},
computed: {
userStatus() {
// return a state or something, but must return something
return ...
},
},
};
I invite you to read this documentation to have better idea about computed property and methods我邀请您阅读此文档以更好地了解计算属性和方法
https://v2.vuejs.org/v2/guide/computed.html#Computed-Properties https://v1.vuejs.org/guide/events.html https://v2.vuejs.org/v2/guide/computed.html#Computed-Properties https://v1.vuejs.org/guide/events.html
注销功能应该在方法对象的顶部,你可以使用 this.logout() 来引用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.