
在 Vue 中实现当 hasrights 为 true 时处理 amt 字段,大于 0 返回 +0,小于 0 返回 -0,当 hasrights 为 false 时返回 ***,可以使用条件语句和模板字符串。
示例代码:
<template>
  <div>
    {{ hasrights ? (amt > 0 ? '+0' : '-0') : '***' }}
  </div>
</template>解析:
- 条件语句 hasrights ? (amt > 0 ? '+0' : '-0') : '***',当hasrights为true时,判断amt的值,如果大于 0 则返回+0,否则返回-0;当hasrights为false时返回***。
- 模板字符串 {{}}用于将表达式的值插入到模板中。
假设有如下数据:
{
   hasrights: true,
   amt: 100
}可以通过定义计算属性来实现需求:
computed: {
  processedAmt() {
    if (this.hasrights) {
      return this.amt > 0 ? `+${this.amt}` : `${this.amt}`;
    } else {
      return '***';
    }
  }
}使用processedAmt计算属性即可获得处理后的amt值。如果hasrights为true并且amt大于0,返回+amt,否则返回amt。如果hasrights为false,则返回'***'。










