猿问

Vue input中如何对属性进行计算

1,需求:需要给input绑定一个属性validata 这个属性的值是后台传递过来的,为列表中i.validType 可能有三种1,2,3, 想把这1,2,3对应换成相应的字符,如:1->a 
2->b, 3->c

2,代码
<input v-bind:validata="validTypeFun(i.validType)" >

尝试过的办法:
a,使用 filter 
<input v-bind:validata="i.validType | validTypeFun " >
但是查了一下 https://segmentfault.com/q/10...
2.0版本是不支持v-bind中使用过滤器的,2.1版本才支持。

b,尝试使用方法method,没有触发事件,也不行

c,尝试使用数组进行映射,
<input v-bind:validata="arr[i.validType+1]" >
在data 中注册
arr:["a","b","c"],
会报错
Property or method "arr" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

求解?


qq_花开花谢_0
浏览 1268回答 1
1回答

神不在的星期二

<input v-bind:validata="arr[i.validType+1]" >可能是arr[i.validType-1]? 而且如果i.validType是字符串的话,可能要parseInt或者可以在请求回调中处理一下数据:let arr = res.data.arrarr.map(a=>{&nbsp; &nbsp; swith(a.validType){&nbsp; &nbsp; &nbsp; &nbsp; case '1': a.validType_EN=a&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }})
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答