gpt4 book ai didi

angular - 错误类型错误 : Cannot read property 'extras' of null

转载 作者:行者123 更新时间:2023-12-04 09:57:34 24 4
gpt4 key购买 nike

这是我的代码,我收到错误“ERROR TypeError: Cannot read property 'extras' of null”

  ngOnInit() {
const navigation = this.router.getCurrentNavigation();
const state = navigation.extras.state as {
api: string;
trace_id: string;
token_id: string;
hotel_code: string;
result_index: string;
};
this.reqObj = {
api: state.api,
trace_id: state.trace_id,
token_id: state.token_id,
hotel_code: state.hotel_code,
result_index: state.result_index
};
}

最佳答案

您可能需要调用 getCurrentNavigation()在构造函数中。您也可以定义一个接口(interface)以避免重复对象类型。尝试以下

export interface Hotel {
api: string;
trace_id: string;
token_id: string;
hotel_code: string;
result_index: string;
}

@Component({
selector: 'my-details',
templateUrl: './details.component.html',
styleUrls: [ './details.component.css' ]
})
export class DetailsComponent implements OnInit {
reqObj: Hotel;

constructor(private router: Router) {
this.reqObj = this.router.getCurrentNavigation().navigation.extras.state as Hotel;
}

ngOnInit() {
}
}

关于angular - 错误类型错误 : Cannot read property 'extras' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61892670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com