this is my code and I am getting the error "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
};
}
You might need to invoke getCurrentNavigation() in the constructor. Also you could define an interface to avoid repeating the object type. Try the following
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() {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With