2

I am new to Angular and I have been following the https://angular.io/start as a guide and I came across an error on the latter part of the passing data to a child component section. the last instructions given by the guide is

To display ProductAlertsComponent as a child of ProductListComponent, add the selector, to product-list.component.html. Pass the current product as input to the component using property binding.

I have done that and added this to my product-list-component.

<app-product-alert [product]="product"></app-product-alert>

but I encountered an error on my stackblitz windows saying that "app-alert-component is not a known element." I tried importing it on the app.module.ts like the answer here says Angular 2 'component' is not a known element but the stackblitz windows just returns to me a blank page.

this is my app.module.ts

@NgModule({
  imports: [
    BrowserModule,
    ReactiveFormsModule,
    RouterModule.forRoot([{ path: '', component: ProductListComponent }])
  ],
  declarations: [
    AppComponent,
    TopBarComponent,
    ProductListComponent,
    ProductAlertComponent
  ],

and this is my app-alert-component.ts

import { Component, OnInit } from '@angular/core';
import { Input } from '@angular/core';
import { Product } from '../products';

@Component({
  selector: 'app-product-alert',
  templateUrl: './product-alert.component.html',
  styleUrls: ['./product-alert.component.css']
})
export class ProductAlertComponent implements OnInit {
  @Input() product!: Product;
  constructor() {}
  ngOnInit() {}

I'd very much appreciate it if you could point me in the right direction on where did I go wrong. or is this an error on the documentation and if so, is there a way to notify them. Thank you.

IAfanasov
  • 4,775
  • 3
  • 27
  • 42
Michael Lim
  • 31
  • 1
  • 4
  • 2
    Hey Michael - welcome to SO! At first glance your code looks fine. Not seeing anything strange there. Can you share your stackblitz maybe? – MikeOne Jun 17 '21 at 08:32
  • 2
    Hi :) this may sound like a silly thing to try, but did you try restarting ng serve? Sometimes when you play around with your AppModule, the dev server doesn't figure it all out. – Will Alexander Jun 17 '21 at 08:36
  • 1
    Hi @MikeOne, thank you for the warm welcome. I found the answer to my question and posted it as an answer. thank you for taking your time to look at my question. cheers! – Michael Lim Jun 17 '21 at 08:37
  • 1
    Hi @WillAlexander it is silly, and I just found it out as the answer to my question. thank you Will, cheers! – Michael Lim Jun 17 '21 at 08:39
  • 1
    Welcome to Angular development, where we restart the dev server WAY more than we should have to ^^ – Will Alexander Jun 17 '21 at 08:44

1 Answers1

1

I just found the answer.

adding the ProductAlertComponent on the declarations of the app.module.ts works but you have to do a refresh on the page.

Michael Lim
  • 31
  • 1
  • 4
  • I wish this was in the Getting Started tutorial, it is the first steps in Angular. app.module.ts: import { ProductAlertsComponent } from './product-alerts/product-alerts.component'; declarations: [ AppComponent, TopBarComponent, ProductListComponent, ProductAlertsComponent ], – Andor Lundgren Aug 26 '21 at 09:43