Reputation: 1691
cartService always logs undefined. I'm not sure if it has to do with the abstract class since I've never used it before and have no idea what it does (I'm updating code which wasn't originally written by me). Using a regular class doesn't solve the problem though...
product-utils.ts
import { CartService } from '../services/cart.service';
import { Store } from '@ngrx/store';
...
export abstract class ProductUtils {
constructor(
private cartService: CartService,
protected store: Store<any>) {}
addToCart() {
this.cartService.addCartFinalProduct(foo); // ERROR TypeError: Cannot read property 'addCartFinalProduct' of undefined
...
}
}
Store is available unlike cartService for some reason.
I'm using Angular 5.
cart.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class CartService {
addCartFinalProduct() {
...
}
}
Upvotes: 0
Views: 185
Reputation: 4838
You're instantiating a cartService in the constructor, which is correct, you're stating that cartService
is of type CartService
, you're not giving it a value. This is the reason the console.log(this.cartService)
is logging as undefined
.
You would need to return something from a method inside cartService and console.log()
that response.
Example
console.log(this.carService.SomeMethodWithAReturn());
It is the same thing as creating a variable like so public variable: string;
and then console.log(this.variable)
, this will also return undefined, until you assign a value to it, like public variable: string = 'random string';
, if you console.log(variable)
now, you will see "random string" in the console.
Upvotes: 1
Reputation: 222592
You need to use this
constructor(
private cartService: CartService,
protected store: Store<any>) {
console.log(this.cartService);
}
Upvotes: 0