我正在尝试在我的angular 9组件中创建一个嵌套数组,以便可以在模板中调用它。我不确定问题是在格式化还是在根本上做错了什么。我不断收到此错误消息:macro(assign_me_bool var)
if(${ARGV})
set(${var} ON)
else()
set(${VAR} OFF)
endif()
endmacro()
assign_me_bool(VAR_3 VAR_1 STREQUAL VAR_2)
这是我的打字稿:
Cannot read property 'push' of undefined
这是我的HTML:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'ngx-analysis',
templateUrl: './analysis.component.html',
styleUrls: ['./analysis.component.scss']
})
export class AnalysisComponent implements OnInit {
persuadables: [{'icon':string, 'desc':string, 'status':string}];
constructor() { }
ngOnInit() {
this.persuadablePush();
}
significance(score, name, arr){
if (score == 1.0){
arr.push({'icon':'checkmark-done-circle', 'desc': 'Very ' + name.toLowerCase(), 'status':'success'}) ;
}
else if (score == 0.5){
arr.push({'icon':'checkmark-circle', 'desc':name, 'status':'info'}) ;
}
else if (score == 0.0){
arr.push({'icon':'close-circle', 'desc': 'Not ' + name.toLowerCase(), 'status':'danger'}) ;
}
}
persuadablePush(){
for( let i=5; i<10; i++ ){
this.significance(
this.insights['consumption_preferences'][0]['consumption_preferences'][i]['score'],// =number
this.insights['consumption_preferences'][0]['consumption_preferences'][i]['name'],// =string
this.persuadables
)
}
}
谢谢您的帮助!
答案 0 :(得分:1)
我认为您正在寻找的(两者都应该起作用)
export class AnalysisComponent implements OnInit {
//persuadables: Array<{'icon':string, 'desc':string, 'status':string}> = [];
//persuadables: {'icon':string, 'desc':string, 'status':string}[] = [];
}
答案 1 :(得分:0)
我认为发生这种情况是因为您编写了类型,但未定义数组。
尝试这样:
export interface Persuadable {
icon: string;
desc: string;
status: string;
}
export class AnalysisComponent implements OnInit {
public persuadables: Array<Persuadable> = [];
}
使用接口并初始化数组。