其实react中没有类似vue的filter直接插进去使用的过滤器
所以我们如果想要使用的话,得自己写一个函数替代。。
1.class中使用例子:
首先:

<span className="join-people">{this.countFormat(item.count)}</span>

其次过滤函数写法:

countFormat = item => {
     let count
     if (typeof (item) === 'number') {  // typeof返回字符串
       count = item + '参与'
     } else {
       count = item
     }
     return count
   }

我的这个需求是判断如果是number,就在后面拼接个参与两个汉字,如果是string,就保留原来不改变。
注:typeof判断数据类型返回字符串,所以这里要判断为"number"
2.函数组件中使用例子:
首先:

<span className="join-people">{countFormat(item.count)}</span>

其次过滤器中代码:

const countFormat = item => {
     let count
     if (typeof (item) === 'number') {  // typeof返回字符串
       count = item + '参与'
     } else {
       count = item
     }
     return count
   }

两者的差别也就只在this指向上面的问题,其他方面的写法其实是一致的

Logo

前往低代码交流专区

更多推荐