A shared resource is an object instance made available to multiple components, allowing them to use common state and behavior. One component can edit the shared value while another displays it. section 18.2.1
Shared discount state
Create one DiscountService instance in ProductTableComponent. Pass it to PaDiscountEditorComponent and PaDiscountDisplayComponent through the discounter input. The editor changes discounter.discount with two-way binding, while the display reads the same property. The service also centralizes discount behavior with getters, a setter, and applyDiscount, which prevents prices from falling below $5. section 18.2.1
Listing 18.2. The contents of the discount.service.ts file in the src/app folder
export class DiscountService {
private discountValue: number = 10;
public get discount(): number {
return this.discountValue;
}
public set discount(newValue: number) {
this.discountValue = newValue ?? 0;
}
public applyDiscount(price: number) {
return Math.max(price - this.discountValue, 5);
}
}
Inspect the complete DiscountService implementation, including its private state, discount accessors, and price-adjustment method. section 18.2.1


