if (this.channelId === ‘378‘ || this.channelId === ‘217‘ || this.channelId === ‘426‘){修改

问:

<td><input type="radio" v-model="checkout" @change="getDateFn" :data-type="item.articleType" :data-channelName="item.channelName" :data-channelId="item.channelId" :data-title="item.title" :data-sourceCategoryId="item.sourceCategoryId" :data-code="item.oneline_news_code" :value="item.id || item.articleId"/> </td>





data字段:
sourceCategoryId: '' // 这个值代表mini类型下 财新闻类型文章




// 单选按钮逻辑
        if (this.channelId === '378' || this.channelId === '217' || this.channelId                                                 === '426') {
          // this.alertTypeOne = 0 // 单选按钮选择 是
          if (this.sourceCategoryId == 101862930) {
            this.alertTypeOne= 1 // 单选按钮选择 否
          } else {
            this.alertTypeOne = 0 // 单选按钮选择 是
          }
        } else {
          this.alertTypeOne= 1 // 单选按钮选择 否
        }

怎么将代码简化?

回答:

你可以简化这段逻辑代码如下:


if (['378', '217', '426'].includes(this.channelId)) {
  this.alertTypeOne = (this.sourceCategoryId == 101862930) ? 1 : 0;
} else {
  this.alertTypeOne = 1;
}


这里用了 `Array.prototype.includes` 方法来检查 `this.channelId` 是否包含在指定的频道ID数组中。然后根据 `this.sourceCategoryId` 的值来设置 `this.alertTypeOne` 的值。

相关推荐

  1. (第38天)RAC 修改 IP 信息

    2024-07-20 22:00:01       50 阅读
  2. 38节: Vue3 鼠标按钮修改

    2024-07-20 22:00:01       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 22:00:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 22:00:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 22:00:01       45 阅读
  4. Python语言-面向对象

    2024-07-20 22:00:01       55 阅读

热门阅读

  1. 23年oppo提前批B组笔试真题-小欧的卡牌

    2024-07-20 22:00:01       18 阅读
  2. Django已经登录但是还是提示登录

    2024-07-20 22:00:01       17 阅读
  3. Go语言入门之函数

    2024-07-20 22:00:01       18 阅读
  4. swift小知识点

    2024-07-20 22:00:01       14 阅读
  5. 项目中MyBatis要引入的内容

    2024-07-20 22:00:01       16 阅读
  6. ccf-csp认证--仓库规划

    2024-07-20 22:00:01       16 阅读
  7. Kraken代码阅读(一)

    2024-07-20 22:00:01       15 阅读