【发布时间】:2018-11-10 01:45:51
【问题描述】:
我在 VueJS 中有我的辅助函数,我可以在 JavaScript 中调用它,但不能在 HTML 标记中使用它。以下是我的 helper.js 函数。
//Helper JS file
const allowNumbers = e => {
var key = e.which || e.keyCode;
if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
// numbers
key >= 48 && key <= 57 ||
// Numeric keypad
key >= 96 && key <= 105 ||
// comma, period and minus, . on keypad
key == 190 || key == 188 || key == 109 || key == 110 ||
// Backspace and Tab and Enter
key == 8 || key == 9 || key == 13 ||
// Home and End
key == 35 || key == 36 ||
// left and right arrows
key == 37 || key == 39 ||
// Del and Ins
key == 46 || key == 45)
return true
e.preventDefault()
return false
}
export {
allowNumbers
}
我在 Vue 组件中导入它,但它给出了“未定义函数”的错误。我的 Vue 文件是。
<!-- Vue component file -->
<input maxlength="4" type="text" @keydown="allowNumbers" />
<script>
import {allowNumbers} from '@/helpers'
</script>
注意:我已经更新了我的代码,但我仍然需要一些方法来直接在 HTML 中使用辅助函数
【问题讨论】:
-
这在很多方面都是错误的,我不知道如何帮助你。我建议你重新阅读 Vue 文档,看看如何使用 Vue。
-
嗨,你为什么这么说?也请解释一下评论。
-
@JacobGoh 我已经更新了我的代码,请立即查看。我仍然需要在 HTML 中直接使用辅助函数。
-
@HasnatSafder 你能解释一下你想做什么吗?您想忽略某些关键事件吗?但它是在文本输入中
-
检查 vuejs.org/v2/guide/events.html 以获取事件修改
标签: javascript function vue.js vuejs2 vue-component