vue踩坑系列之“在input 中使用filters 局部过滤器”

vue踩坑系列之“在input 中使用filters 局部过滤器”

​ 最近在项目中遇到需要对Input框中v-model绑定的枚举值进行过滤展示到页面上,万事具备,刷新页面后发现filters并没有起作用,控制台还报错:

[Vue warn]: Property or method "filterTime" 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.

​ 于是乎,我就重新研究了一下vue.js的过滤器filters,发现filters只能用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持)。

中文官网链接:https://cn.vuejs.org/v2/guide/filters.html

解决方案:使用计算属性

​ 一个小demo

<template>
	<input v-model="filterCardType" placeholder="证件类型" />
</template>

<script>
    export default {
        data() {
			return {
				agent_detail: {
                    agent_detail:1,
                }, 
			};
		},
        computed:{
			"filterCardType"(){
				if(this.agent_detail.cardType==0){
					return "二代身份证"
				}else if(this.agent_detail.cardType==1){
					return "护照"
				}else if(this.agent_detail.cardType==2){
					return "港澳通行证"
				}else if(this.agent_detail.cardType==3){
					return "台湾通行证"
				}else if(this.agent_detail.cardType==4){
					return "香港永久性居民身份证"
				}else if(this.agent_detail.cardType==5){
					return "军官证"
				}
			}
		}
    }
</script>

"登上高峰后,你会发现还有更多的山峰要翻越"

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值