I'm trying to build a admin panel using Angular. Please do note that, this is my first time using Angular and first time on OOP constructs.
I have a page where I list all the product types, by selecting or rather clicking on any of the product type. I redirect them to another page with the brands for the respective product.
So, when I searched the internet to check what is the best way of sharing data across multiple components, I found BehaviourSubject, and I did successfully manage to redirect the user from the products
page to the brands
page. Now I wanted to route the user to a new page from the brands
page after they select the brands.
So far, I managed to do the BehaviourSubject with just one variable of type string
which is selecting one product from the products page. Now I have an option for my users to select multiple brands.
How can I pass this data to the next page? And when I select the brands, will I still be able to access my product
value using the BehaviouSubject. I was thinking to create a class and update the last value to the class, which I couldn't do it unfortunately.
CODE
@Injectable()
export class UserActionService {
private product;
private currentProduct;
constructor() {
this.product = new BehaviorSubject<UserActions>(null);
this.currentProduct = this.product.asObservable();
}
changeProduct(product: UserActions) {
this.product.lastProduct.next(product);
}
}
export class UserActions {
private lastProduct: string;
private lastBrands: Array<string>;
}
This code didn't throw me any error on the tslint, but on the browser it throws me an error:
Cannot read property 'next' of undefined
I don't know if I'm doing the right thing, any help will be much appreciated.