Reputation: 997
i have a Singleton service class in Angular :
Upvotes: 0
Views: 38
Reputation: 3834
You should create an instance of your class (in this case your service) and then you can access any public methods and variables in the root class. in order to create an instance of your class, you need your constructor
to know about this.
constructor(private router:Router, private tripService:TripService) { }
now an instance of your class is available in your component and you can access that using this.tripService
. keep that in mind the tripService
is up to you and you can declare any name you desire.
export class TripDetailsPage implements OnInit {
constructor(private router:Router, private tripService:TripService) { }
ngOnInit() {}
}
Extra note: if you want your service not to be a singleton service (which you can use access in your whole application), you need to provide your component in your service.
Upvotes: 0
Reputation: 2925
export class TripDetailsPage implements OnInit {
currentTrip:Trip = new Trip("","")
constructor(private router:Router, private tripService:TripService) { }
ngOnInit() {
this.currentTrip = this.tripService.getCurrentTrip();
console.log(this.currentTrip);
}
switchToTab(path: string) {
this.router.navigate(['home/trips/trip-details/' + path]);
}
}
Upvotes: 1