I got the following error: Cannot set property 'id' of undefined at UserComponent.push../src/app/users/user/user.component.ts.UserComponent
TS Code
import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
@Component({
selector: "app-user",
templateUrl: "./user.component.html",
styleUrls: ["./user.component.css"],
})
export class UserComponent implements OnInit {
user: { id: number; name: string };
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.user.id = +this.route.snapshot.params['id'];
}
}
Template:
<p>User with ID {{user.id}} loaded.</p>
<p>User name is {{user.name}}</p>