【发布时间】:2023-03-08 04:32:01
【问题描述】:
如何在 Vue 模板中使用 JavaScript 内置函数?
{{ eval(item.value.substring(2)) }}
我想在 {{}} 中使用 JS 函数 eval(),但它显示了许多错误,例如:
[Vue warn]: Property or method "eval" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
[Vue warn]: Error in render: "TypeError: _vm.eval is not a function"
vue.esm.js?efeb:1906 TypeError: _vm.eval is not a function
【问题讨论】:
-
尝试在组件代码中调用 eval,然后在模板代码中使用结果。
-
我知道我可以在方法中调用它,但是我想尝试在模板中直接使用JavaScript函数。
-
但是为什么呢?鉴于在渲染代码中运行计算是不好的做法?
-
我只是不想在我的代码中添加更多行。我喜欢短代码。
-
使用文档建议的计算属性。这是一种很好的做法,可以使您的模板代码保持简短。
标签: javascript vue.js templates