I have a parent component that contains a massive of objects:
export const myWalls: MyWall[] = [
{
sign: 'COTTON CANDY',
photo1: 'assets\walls.jpg',
photo2: 'assets\1.jpg',
photo3: 'assets\2.jpg',
id:1,
mySong: [
{
name: 'firstsong',
cover: 'assets\1.jpg',
song: 'assets\audio\11.mp3',
},
{
name: 'secondsong',
cover: 'assets\1.jpg',
song: 'assets\audio\12.mp3',
},
]
},
{
sign: 'NEON',
photo1: 'assets\3.jpg',
photo2: 'assets\4.jpg',
photo3: 'assets\5.jpg',
id:1,
mySong: [
{
name: 'thirdsong',
cover: 'assets\1.jpg',
song: 'assets\audio\11.mp3',
},
{
name: 'fourthsong',
cover: 'assets\1.jpg',
song: 'assets\audio\11.mp3',
},
]
}
]
every object is connected to a certain route by its id field. and by grabbing this id i am supposed to get the rest of the properties of an object. for that i have a constructor in a child component. But even after initialization the object stays undefined and it ruins everything that follows. can anyone please say what could be the problem?
export class FirstInComponent{
public object?: MyWall;
constructor(private route: ActivatedRoute) {
this.route.params.subscribe((params: Params) => {
this.object = myWalls[params['id']-1];
console.log(typeof this.object)
})
}
i tried to use OnInit instead of constructor but nothing changes
public object?: MyWall;
constructor(private route: ActivatedRoute) {}
ngOnInit(): void {
this.route.params.subscribe((params: Params) => {
this.object = myWalls[params['id']-1];
console.log(typeof this.object)
})
}
dog-pineapple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.