Javascript 直接在HTML标记中使用Vue JS自定义帮助程序函数
我在VueJS中有我的helper函数,我可以在JavaScript中调用它,但不能在HTML标记中使用它。下面是我的helper.js函数Javascript 直接在HTML标记中使用Vue JS自定义帮助程序函数,javascript,function,vue.js,vuejs2,vue-component,Javascript,Function,Vue.js,Vuejs2,Vue Component,我在VueJS中有我的helper函数,我可以在JavaScript中调用它,但不能在HTML标记中使用它。下面是我的helper.js函数 //Helper JS file const allowNumbers = e => { var key = e.which || e.keyCode; if (!e.shiftKey && !e.altKey && !e.ctrlKey && // numbers ke
//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
}
//Helper JS文件
常量AllowNumber=e=>{
var key=e.which | e.keyCode;
如果(!e.shiftKey&&!e.altKey&&!e.ctrlKey&&
//数字
键>=48&&key=96&&key
对于要从vue模板访问数据和函数的问题,请确保属性在“数据”选项中或在“方法”下列出。
假设我们有下面的helper函数
//Helper JS file
const utilFunc = num => {
if (num < 0) {
return "negetive";
}
return "posetive";
};
export { utilFunc };
//Helper JS文件
const utilFunc=num=>{
if(num<0){
返回“negative”;
}
返回“posetive”;
};
导出{utilFunc};
在yout vue组件中
<template>
<div id="app">
{{val}}
{{proxyUtilFunc(-9)}}
</div>
</template>
<script>
import { utilFunc } from "@/helpers";
export default {
name: "App",
methods: {
proxyUtilFunc: utilFunc
},
data() {
return {
val: utilFunc(5)
};
}
};
</script>
{{val}}
{{proxyUtilFunc(-9)}
从“@/helpers”导入{utilFunc}”;
导出默认值{
名称:“应用程序”,
方法:{
proxyUtilFunc:utilFunc
},
数据(){
返回{
val:utilFunc(5)
};
}
};
但正如我所注意到的,您正在尝试使用动态样式值呈现HTML,这正是我们需要的原因
因此,我强烈建议您将其重构为其上的微调器组件
拥有
导出默认值{
姓名:“MySpinner”,
道具:{
尺寸:{
类型:编号
}
}
};
这里有一个可供试验的方法。
NB:考虑检查官方代码重用部分
使用,和vue
用于代码可重用性
对于要从vue模板访问数据和函数的问题,请确保属性在“数据”选项中或在“方法”下列出。
假设我们有下面的helper函数
//Helper JS file
const utilFunc = num => {
if (num < 0) {
return "negetive";
}
return "posetive";
};
export { utilFunc };
//Helper JS文件
const utilFunc=num=>{
if(num<0){
返回“negative”;
}
返回“posetive”;
};
导出{utilFunc};
在yout vue组件中
<template>
<div id="app">
{{val}}
{{proxyUtilFunc(-9)}}
</div>
</template>
<script>
import { utilFunc } from "@/helpers";
export default {
name: "App",
methods: {
proxyUtilFunc: utilFunc
},
data() {
return {
val: utilFunc(5)
};
}
};
</script>
{{val}}
{{proxyUtilFunc(-9)}
从“@/helpers”导入{utilFunc}”;
导出默认值{
名称:“应用程序”,
方法:{
proxyUtilFunc:utilFunc
},
数据(){
返回{
val:utilFunc(5)
};
}
};
但正如我所注意到的,您正在尝试使用动态样式值呈现HTML,这正是我们需要的原因
因此,我强烈建议您将其重构为其上的微调器组件
拥有
导出默认值{
姓名:“MySpinner”,
道具:{
尺寸:{
类型:编号
}
}
};
这里是一个要尝试的。
NB:考虑检查官方代码重用部分
使用、和vue
用于代码可重用性
这在很多方面都是错误的,我不知道如何帮助您。我建议您重新阅读Vue文档,看看如何使用Vue。您好,您为什么这么说?请解释一下注释。@JacobGoh我已经更新了代码,请现在查看。我仍然需要一些方法来直接在HTML中使用helper函数。@HasnatSafder您能解释一下是什么吗你想做什么?你想忽略某些关键事件吗?但它是在事件修改的文本输入签出上。这在很多方面都是错误的,我不知道如何帮助你。我建议你重新阅读Vue文档,看看如何使用Vue。嗨,你为什么这么说?请解释一下注释。@JacobGoh我已经更新了我的代码,请查看它现在。我仍然需要一些方法在HTML中直接使用helper函数。@HasnatSafder你能解释一下你想做什么吗?你想忽略某些关键事件吗?但它是在文本输入上的,用于事件修改。谢谢,请查看我问题中的更新代码。所以基本上我不能直接使用它。谢谢,请查看更新我的问题是d代码。所以基本上我不能直接使用它?