Javascript 不向需要参数的方法传递参数
我尝试不向需要参数的方法传递任何参数。到目前为止,我已经做到了这一点,但我似乎无法让它发挥作用:Javascript 不向需要参数的方法传递参数,javascript,vue.js,vuejs2,vue-component,Javascript,Vue.js,Vuejs2,Vue Component,我尝试不向需要参数的方法传递任何参数。到目前为止,我已经做到了这一点,但我似乎无法让它发挥作用: // HTML // this one doesn't pass an argument <el-button @click:search>Button 1</el-button> // this one does pass an argument <el-button @click:search('hey')>Button 2</el-button&
// HTML
// this one doesn't pass an argument
<el-button @click:search>Button 1</el-button>
// this one does pass an argument
<el-button @click:search('hey')>Button 2</el-button>
// JS
method: {
search (searchBy) {
// if there's a value
if( searchBy ) {
console.log(searchBy);
// if there's no value output this intead
else
console.log("nothing to search by");
}
}
//HTML
//这一条没有通过辩论
按钮1
//这是一个论点
按钮2
//JS
方法:{
搜索(searchBy){
//如果有价值的话
如果(搜索方式){
console.log(searchBy);
//如果这个intead没有输出值
其他的
console.log(“无需搜索”);
}
}
尝试搜索()而不是搜索
<el-button @click:search()>Button 1</el-button>
按钮1
即使您没有关闭
如果
括号您是否尝试过只使用@click:search()
?它不起作用的原因(除了问题中的语法错误)这是因为没有指定参数的单击处理程序默认情况下将接收事件
对象。ohhh这是我想要的解释。谢谢@BertI,虽然只需传递方法名就足够了,而且会自动处理没有参数的事情passed@AlfieMcNarutoad使用方法名将返回th函数定义和调用它必须使用括号()