这是需要修复的material 标识的代码,它应该已经保存了从零开始的索引,并且递增,它不应该使用indexOf或color index,属性应该更改为number而不是string,这样它就不会循环查找索引,所以代码更干净、更快.

 badgesColorSet = ['#ffff00', '#f51307', '#0cc902'];
  badgesColorSelected: string;

  getIteriateColor() {
    if (!this.badgesColorSelected) {
      this.badgesColorSelected = this.badgesColorSet[0];
    } else {
      const colorIndex = this.badgesColorSet.indexOf(this.badgesColorSelected);
      if (colorIndex + 1 > this.badgesColorSet.length - 1) {
        this.badgesColorSelected = this.badgesColorSet[0];
      } else {
        this.badgesColorSelected = this.badgesColorSet[colorIndex + 1];
      }
    }
  }

我试着让它更快,并删除了的索引,但没有成功

推荐答案

getIteriateColor(){

if (!this.badgesColorSelected) {
  this.badgesColorSelected = 0;
} else {
  const colorIndex = this.badgesColorSelected;
  if (colorIndex + 1 > this.badgesColorSet.length - 1) {
    this.badgesColorSelected = this.badgesColorSet[0];
  } else {
    this.badgesColorSelected = this.badgesColorSet[colorIndex + 1];
  }
}
console.log('current color is: ', this.badgesColorSelected);

return this.badgesColorSelected;

}

Html相关问答推荐

在浮动元素旁边垂直居中

如何在htmlAngular 17的@for中使用索引

滚动平滑在笔记本电脑上不起作用,但在Nextjs网站的手机浏览器中起作用

如何使不断增长的div不溢出其包含固定div的父级

HTML中的ARIA标签:在元素内部还是外部?(&Q;

如何使链接组件内的内容不重新路由Next.js13

多次使用组件时计数Angular 14中嵌套数组的指令

太多的 Web 视图实例导致手机过热 Swift

Div 容器不会遵守边距、填充或间隙

如何使用 Tailwind CSS 分隔导航栏中的元素?

为什么可滚动弹性项目需要 flex-basis: 0 ?

将现有内容拆分为三列或部分

为什么使用 Css 只能看到 1 列而不是 3 列?

为什么 css position:fixed 不适用于对话框标签?

将 div 放在图片旁边而不是下方

让 Iframe 覆盖整个页面

嵌套固定定位的 Div 滚动条渗透到前景中的 Div

html tailwindcss 给 li 标签添加边框

如何自定义具有白色透明背景的光标图像?

CSS:如何在模糊的背景上剪切文本?