Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

vue中数据格式问题

后台返回的是字符串1,2,3,4,5
我想保留钱四个并且转化为数组格式,这样写数据为空的时候偶尔会报split错,前面加了判断也不行,还有别的方法吗请问
<view v-if="item.tags.indexOf(',') != -1" v-for="(aItem,index) in item.tags.split(',').slice(0,4)" :key="index">{{aItem}}</view>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

问题解决

// 最好是通过计算属性或者请求响应后先计算
// 别同时用v-if和v-for,这样会降低数据渲染性能
computedTagList() {
  const list = item.tags || '';
  return list.replace(/,/g, '').substring(0, 4).split('');
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...