An student
An student

Reputation: 392

Getting "Property 'route' does not exist on type 'typeof" using angular10

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);
    });
}
}

Upvotes: 0

Views: 2104

Answers (1)

Bellash
Bellash

Reputation: 8184

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');

Upvotes: 1

Related Questions