关于某些情况Vue的过滤器无法使用

Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化。过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持)。
<router-link :to=”{ path: item.type | changeToNum, query: { id: item.fid }}”>
//…
</router-link>

//…

filters: {
changeToNum: function(value) {
//do something here…
}
}
这种情况下控制台会报错:vue.runtime.esm.js?2b0e:587 [Vue warn]: Property or method “isFunction” 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.发现这种情况下好像不能使用过滤器…于是试着直接写一个函数isFuction(item.type),然后在methods中定义该函数进行操作即可,代码如下:
<router-link :to=”{ path: isFuction(item.type), query: { id: item.fid }}”>
//…
</router-link>
//…

methods: {
isFunction(value) {
//do something here…
}
}
直接写一个函数,这样就可以在这种不能使用过滤器的情况下进行一些类似操作了。——by 2019.03.05

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理