简体   繁体   English

从Vue中的选择元素中选择选项后如何调用函数?

[英]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.

相关问题 在选择框中选择选项时如何调用函数 - How to call a function while selecting an option in select box 从“选择文件”窗口中选择文件并将其关闭后,如何调用 JavaScript 函数? - How do I call a JavaScript function after selecting a file from the Select File window and closing it? 在选择元素中找到一个选项,然后选择它 - Finding an option in a select element, and selecting it 选择选项后如何在select2的下拉列表中隐藏选项 - How to hide option in dropdown in select2 after selecting the option 从 select 元素中选择选项时,控制台日志加倍 - Console log is being doubled when selecting an option from a select element 如何从html选择选项onChange调用php函数? - How to call php function from html select option onChange? 如何从 Select 选项中调用 JavaScript function - How to call a JavaScript function from Select Option with Html Vue 2 - 如何选择正确的<select>从 db 获取数据后的选项 - Vue 2 - How to select the correct <select> option after the data is fetched from db 仅在从“选择类别”标签中选择一个选项后,如何显示输入选项? - How to display input options only after selecting an option from the 'select class' tag? 选择选项后如何取消突出显示 select 元素? - how to unhighlight select element after option is choosed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM