I have a few lines of code taken from an Italian Angular book entitled "Angular 100% operational" that I don't understand. The text reports to paragraph 5.4: "It often happens that the values to be dynamically added are retrieved from an external source that packs them into an array. In Angular it is also possible to use the following notation:"
@ Component({
selector: 'ca-news',
template: `<p [ngClass]="mieclassi" class="big-text"></p>`,
styleUrls: ['./miostile.css']
}
) export class NewsComponent {
mieclassi: Array < string > ;
constructor() { // applico due attributi di classe
this.mieclassi = ['text-center', 'light-blue'];
}
}
But is it correct in your opinion to insert square brackets here?
this.mieclassi = ['text-center', 'light-blue'];
Because in the previous example of the book, the author didn't insert them, the book indicates:
@Component({
selector: 'ca-news',
template: `<p [ngClass]="mieclassi" class="big-text">News con testo molto piccolo</p>`,
styleUrls: ['./miostile.css']
}) export class NewsComponent {
mieclassi: string;
constructor() {(
this.mieclassi = 'text-center light-blue';
}
}
}
and it works! So what's the meaning of [] in the first example?
this.mieclassi = ['text-center', 'light-blue'];
Then the author writes about the use of Css in an Array and introduces this notation with square brackets which, however, does not work for me, this is the compiler's answer:
Failed to compile.
src/app/news-component-css/news-component-css.component.ts:17:5 - error TS2322: Type 'string[]' is not assignable to type 'string'.
this.mieclassi = 'text-center light-blue';
Thanks for any suggestions, greetings from Italy
question from:
https://stackoverflow.com/questions/66050410/angular-css-and-notation-related-to-array-with-brackets 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…