0

I am trying to route the data from service to a component using the following codes. It shows me error

"Property 'route' does not exist on type 'typeof PaymentService'.ts"

"Declare static property route"

import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { Router} from '@angular/router';
@Injectable({
  providedIn: 'root',
})

export class PaymentService {    
    constructor(private route:Router) { }
    static verifyPayment(payload: any) {
    const axios = require('axios');

let data = {
    'token': payload.token,
    'amount': payload.amount,

 };

axios.post(environment.api_url+"set_appointment_with_payment", data)
    .then(response => {
        if(response.data.paymentStatus=== "Completed"){
            this.route.navigate('/Bill');
        }else{
            alert('Something went wrong! Please try again.');
        }
        
    })
    .catch(error => {
        console.log(error);
    });
}
}
1
  • You can't get an instance property from a static method Commented Jun 16, 2021 at 11:45

1 Answer 1

1

Not a good practice to call a message from within the constructor, but you can do it like this

 export class PaymentService {   

  static _route:Router;

  constructor(private route:Router) { 
     PaymentService._route = route;
  }

then you can use the _route object inside static method ONLY after initialization.

then use it in your static method instead of this.route.

        PaymentService._route.navigate('/Bill');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. Will you please specify what should be initialized here? I just have to navigate to another page using this._route.navigate(['/Bill']);

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.