0

I want to parse API response to another component

This is code from 'detail_item.component.ts', this code sent data to route "bookitem" in 'book_item.component.ts'

onSubmit(){
    this.appService.addItem(this.item).subscribe(item => {
      if(item.status ==200){
        this.router.navigate(['/bookitem'], {queryParams: {data: item.data}});
      }
    })
}

this is code from 'book_item.component.ts'

ngOnInit() {
    this.routeActive.queryParams.filter(params => params.data).subscribe(params => {
       this.book_item = params.data;
    });
}

when I console.log(this.book_item), what I got from it is [object object]. It should be contain json data.

2
  • add stackblitz.com example or more information about response you get Commented Sep 5, 2018 at 3:57
  • by just looking at the code you have - it looks like you item.data is an object, not primitive object. Commented Sep 5, 2018 at 5:34

2 Answers 2

0

If the type of item.data is object you need to convert it to string first which is like

onSubmit(){
    this.appService.addItem(this.item).subscribe(item => {
      if(item.status ==200){
        this.router.navigate(['/bookitem'], {queryParams: {data: 
JSON.stringify(item.data)}});
      }
    })
}

then you can parse it when you read it from the route on the other component

 ngOnInit() {
     this.book_item = JSON.parse(this.route.snapshot.queryParams['data']);
          }
Sign up to request clarification or add additional context in comments.

Comments

0

You got [object object] that is completely fine due to you are trying to pass object in query string. Further more you can't pass object in query string you can pass value that is convertible to string easily like number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.