Accessing a variable from one component to another

I am trying to do some kind of angular project but im not in web development. Its a simple store.
I wanted to get info from sqlite from Play Framework. It works.

After getting it in angular and displaying it. His job is too. Services:

@Injectable()
export class ProductService {

constructor(private http: Http) { }

getProducts() {
  const headers: Headers = new Headers();
  headers.append('Accept', 'application/json');
  headers.append('Content-Type', 'application/json');

  const options = new RequestOptions({headers: headers});

  return this.http.get('http://localhost:9900/', options)
  .map(response => <Product[]>response.json());
 } 
}

      

And the place when I take all the groceries:

export class ProductComponent implements OnInit {

public products: Product[];
actualProduct: Product;
productForm: FormGroup;
public quantity: number;
private activeFilter: string = "reset";
public allFilters: Array<string>;


constructor(private productService: ProductService,private router:Router) {
  this.allFilters =  ['ocean', 'river', 'aquarium', 'reset'];
}

ngOnInit() {
  this.productService.getProducts().subscribe(data => this.products = data);
}

public getActualProduct() {
  return this.actualProduct;
}

clickedProduct(product) {
  this.actualProduct = product;
  let link = ['/detail', product.prodId];
  this.router.navigate(link);
}

public setFilter(filter: string) {
 this.activeFilter = filter;
 }
}

      

So my question is, how can I access arrays in other components? Cause now when I click on a button with clickedProduct function it works fine. It appears in the console object object. But when I try to use a product to display product details in another component, I am failing:

 export class ProductDetailComponent implements OnInit {

 selectedProduct: Product;
 products: Product[];
 quantity: number;

 constructor(
 private productService:ProductService,
 private productComponent: ProductComponent,
 private route:ActivatedRoute,
 private location:Location,
  // private cartStore: CartStore
 ) {  }

ngOnInit() {
    this.selectedProduct = this.productComponent.getActualProduct();
}

addToCart(product) {
  console.log(this.selectedProduct)
  console.log(product)
  //this.cartStore.addToCart(product, this.quantity || 1)
}

goBack() {
    this.location.back()
 }
}

      

How can I get the actualProduct variable in ProductDetailComponent or how can I get an array of all products? I tried using "@Input () actualProduct: Product" in the productComponent, but it doesn't actually work. Thanks for the help.

+3


source to share


1 answer


This has a lot to do with how these components relate to each other.

If a parent-child relationship exists , you can pass information to the child component using properties @Input

and listen for child events on the parent object using properties @Output

.



If no such communication exists between components (for example, routable components ), then the most common option is to create a service that manages the flow of information / events between components.

For more information see docs

+2


source







All Articles