3

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.

5
  • 2
    Hey Michael - welcome to SO! At first glance your code looks fine. Not seeing anything strange there. Can you share your stackblitz maybe? Commented Jun 17, 2021 at 8: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. Commented Jun 17, 2021 at 8: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! Commented Jun 17, 2021 at 8:37
  • 1
    Hi @WillAlexander it is silly, and I just found it out as the answer to my question. thank you Will, cheers! Commented Jun 17, 2021 at 8:39
  • 1
    Welcome to Angular development, where we restart the dev server WAY more than we should have to ^^ Commented Jun 17, 2021 at 8:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 ],

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.