错误TypeError:无法读取null的属性“ extras”

时间:2020-05-19 13:47:58

标签: angular

这是我的代码,并且出现错误“ ERROR TypeError:无法读取null的属性'extras'”

  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
    };
  }

3 个答案:

答案 0 :(得分:0)

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

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() {
  }
}

答案 1 :(得分:0)

您在构造函数中包括了正确的路由器吗?喜欢:

import { Router } from '@angular/router';

constructor(public router: Router)
{
    const navigation = router.getCurrentNavigation();
}

答案 2 :(得分:0)

如果 Input rate = numInputRows (or batch size )/ Trigger Interval in secs Processing Rate = numInputRows (or batch size )/ Batch Duration in secs state 一起使用,它会被忽略... (Angular - Issue with routerLink and state)