简体   繁体   English

Angular scss 设计仅适用于一个组件

[英]Angular scss design works only on one component

I have a component, which creates many buttony next to each other.我有一个组件,它创建了许多彼此相邻的按钮。 They all need to have a certain style to them.他们都需要有一定的风格。 They also all have a different color randomly assigned to them based on their class.他们还根据他们的 class 随机分配了不同的颜色。

My problem now is, that when I add the component to one of my other component html files, it works just as I want it to.我现在的问题是,当我将该组件添加到我的另一个组件 html 文件之一时,它可以正常工作。 But if I want to add it to another one, the design is completely off但是如果我想把它加到另一个上,这个设计就完全不对了

Just as the css isnt working on the other components..正如 css 没有在其他组件上工作一样......

But it is still the same code, because it's an imported component.但它仍然是相同的代码,因为它是一个导入的组件。 So I dont' get how it doesn't work...所以我不明白它是如何不起作用的……

Here is my code:这是我的代码:

--> status-bar.component.html --> status-bar.component.html

<div *ngFor="let button of [1,2,3,4,5,6,7,8,9,10]; index as i">
    <button id={{i}}  [ngStyle]="getClassrandom(i)"></button>
  </div>

--> status-bar.component.ts --> status-bar.component.ts

import { Component, Input, OnInit, Output, EventEmitter} from '@angular/core';

@Component({
  selector: 'status-bar',
  templateUrl: './status-bar.component.html',
  styleUrls: ['./status-bar.component.scss']
})


export class StatusBarComponent implements OnInit {


  getClassrandom(i: number): any {
    const classesArray = [
      'status__button status__button--red',
      'status__button status__button--yellow',
      'status__button status__button--green'];
    const element = document.getElementById(i.toString());
    element.className = classesArray[Math.floor(Math.random() * 3)];
  }

  constructor() {
  }
  
    ngOnInit(): void {
    }

  }

---> status-bar.component.scss ---> status-bar.component.scss

.status__button {
    float:left;
    height: 80px;

    width: 25px;
    border-radius: 10px;
    border: 1px solid rgb(255, 255, 255);
    text-align: center;
    -webkit-appearance: none;
    -moz-appearance: none;
    margin: 0;
    box-shadow: 0px 3px 5px 0px rgba(0, 0, 0, 0.2);
    cursor: pointer;
}

.status__button--green {
    background: limegreen;
    box-shadow: 0px 5px 7px 0px rgba(0, 0, 0, 0.2);
    opacity: 0.5;
    &:hover {
        opacity: 1;
    }
}
.status__button--yellow {
    background: yellow;
    box-shadow: 0px 5px 7px 0px rgba(0, 0, 0, 0.2);
    opacity: 0.5;
    &:hover {
        opacity: 1;
    }
}

.status__button--red {
    background: red;
    box-shadow: 0px 5px 7px 0px rgba(0, 0, 0, 0.2);
    opacity: 0.5;
    &:hover {
        opacity: 1;
    }
}

I am using this component in other components like test-bar.component.html or status-bar.component.html我在其他组件中使用此组件,例如 test-bar.component.html 或 status-bar.component.html

The parent component is status.component, which imports all the other ones.父组件是 status.component,它导入了所有其他组件。 That's where the status-modules are as well.这也是状态模块所在的位置。

I hope anyone has an idea on what the solution could be.我希望任何人都知道解决方案是什么。

You're mixin the concept of ngStyle, ngClass and the property className你混合了 ngStyle、ngClass 和属性 className 的概念

You can use您可以使用

<!--see that it's ngClass-->
<div *ngFor="let button of [1,2,3,4,5,6,7,8,9,10]; index as i">
    <button id={{i}}  [ngClass]="getClassrandom()"></button>
</div>

And

  getClassrandom(): string{
    const classesArray = [
      'status__button status__button--red',
      'status__button status__button--yellow',
      'status__button status__button--green'];
    //see that return a string, not change the "clasname"
    return classesArray[Math.floor(Math.random() * 3)];
  }

BTW: Instead pass the index "i" and use document.getElementById, (If you has any element with the same "id" the app crash) You can use a template reference variable顺便说一句:而是传递索引“i”并使用 document.getElementById,(如果你有任何具有相同“id”的元素应用程序崩溃)你可以使用模板引用变量

<div *ngFor="let button of [1,2,3,4,5,6,7,8,9,10]; index as i">
    <!--NOTE that it's an example, you should not use [ngStyle]-->
    <button #statusbutton [ngStyle]="getClassrandom(statusbutton)">
    </button>
</div>

getClassrandom(element: HTMLElement): any {
    const classesArray = [
      'status__button status__button--red',
      'status__button status__button--yellow',
      'status__button status__button--green'];
    element.className = classesArray[Math.floor(Math.random() * 3)];
  }

Well, really use a function in a *ngFor is not a great idea.好吧,真的在 *ngFor 中使用 function 并不是一个好主意。 You can use ViewChildren您可以使用 ViewChildren

@ViewChildren('buttonstatus') buttons:QueryList<ElementRef>
//and in ngAfterViewInit
ngAfterViewInit()
{
       const classesArray = [
          'status__button status__button--red',
          'status__button status__button--yellow',
          'status__button status__button--green'];
       this.buttons.forEach(x=>x.nativeElement.classNameclassesArray[Math.floor(Math.random() * 3)];=
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM