首页 > 解决方案 > 变量值未在打字稿中更新

问题描述

export class Test implements OnInit {
    getRoleTypeValuesArray:any = [];
    array = [];
    constructor(private ls: myService) {}
    ngOnInit() {
      this.ls.testApi().subscribe(y => {
            this.getRoleTypeValues = y;
            this.getRoleTypeValues.map(y=>{
                var obj = { value: y.id, label: y.roleName }
                this.getRoleTypeValuesArray.push(obj);
            });
            this.array = this.getRoleTypeValuesArray;
            console.log("test1:",this.array);
        });
        console.log("test2:",this.array);
    }
}

我在控制台中获取值test1,但不是在test2控制台之外。这段代码有什么问题?

标签: angulartypescriptangular7subscribe

解决方案


推荐阅读