0

Even though I import and export the correct modules and components, the Angular compiler throws an error when I try to use a custom component from a custom module, inside a custom component inside a different module.

button.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

//the button has selector: j-button
import { ButtonComponent } from './button.component';

@NgModule({
  declarations: [ButtonComponent],
  imports: [CommonModule],
  exports: [ButtonComponent],
})
export class ButtonModule {}

When I try to use the button in the main app, it works fine:

app.component.html

<j-button>HELLO</j-button>

But I have a different module:

color-picker.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ColorPickerComponent } from './color-picker.component';
import { ButtonModule } from '../button/button.module';


@NgModule({
  declarations: [ColorPickerComponent],
  imports: [CommonModule, ButtonModule],
  exports: [ColorPickerComponent],
})
export class ColorPickerModule {}

in the template I try to use the component but I get an error

color-picker-component.html

<j-button>HELLO</j-button>

with error

src/app/color-picker/color-picker.component.html:8:9 - error NG8001: 'j-button' is not a known element:
1. If 'j-button' is an Angular component, then verify that it is part of this module.

How do I fix this?

PiwiTheKiwi
  • 129
  • 1
  • 14
  • Have you tried rebuild and restart your application? Sometimes, Angular fails to recognize the newly added module. – skouch2022 Oct 01 '22 at 19:54
  • everything looks good, can you try to reproduce it in stackblitz and provide the link here – Exception Oct 02 '22 at 06:15
  • I found the solution, I wanted to render a 'button' inside my 'colorpicker' and then use the colorpicker module inside my main app. I needed to import the button module in my main app as well. Why did I need to do this, don't other modules have external dependencies that i don't need to worry about? – PiwiTheKiwi Oct 02 '22 at 08:11

0 Answers0