类型 'any[]' 不能分配给类型 'never'

时间:2021-02-07 01:13:48

标签: angular typescript

我正在使用 Angular 在我总是收到这个错误并且 ng build 不起作用之后我升级了 angular cli。那是相关的TS错误吗?我该如何解决这个问题?

错误是

<块引用>
app/analyse/demography/age/age.component.ts:18:9 - error TS2531: Object is possibly 'null'. 
18 this.route.parent.params.subscribe(params => {
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';


@Component({
    selector: 'app-age',
    templateUrl: './age.component.html',
    styleUrls: ['./age.component.scss']
})
export class AgeComponent implements OnInit {

    constructor(private route: ActivatedRoute, private router: Router) {
    }

    ngOnInit(): void {
        this.route.parent.params.subscribe(params => {

        })           
    }

}

另一个问题是我想将对象推送到数组

app/analyse/neighborhood/neighborhood.component.ts:169:74 - error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'.
                                           
this.barChartLabels.push(response.result[0].label[a].n);

1 个答案:

答案 0 :(得分:0)

你可以输入“object?.object?.method(...)”,如果一个对象为空,它会停止链,摆脱空警告。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining