【发布时间】:2019-11-02 12:46:44
【问题描述】:
我正在尝试在我的组件方法之一中为函数设置默认参数值,例如:
methods: {
myFuntion(isAction = false) {}
}
但是当调试 isAction 的值时,我得到一个“MouseEvent”?
【问题讨论】:
标签: vue.js vue-component
我正在尝试在我的组件方法之一中为函数设置默认参数值,例如:
methods: {
myFuntion(isAction = false) {}
}
但是当调试 isAction 的值时,我得到一个“MouseEvent”?
【问题讨论】:
标签: vue.js vue-component
我找到了解决方案。看起来事件也默认传递给方法。因此,您可以在方法函数上设置默认参数值,例如:
methods: {
myFuntion(event, isAction = false) {
// isAction will be false by default
// event will have the contain the event object
}
}
【讨论】:
function 并使用:myFuntion(event, isAction = false) {
<button @click="myFuntion">Default Action</button>
<!-- isAction will be event -->
<button @click="myFuntion()">Default Action</button>
<!-- isAction will be false -->
<button @click="myFuntion(true)">Special Action</button>
<!-- isAction will be true -->
【讨论】: