Component selector from angular library is not a known element

Viewed 1115

i've created simple angular library exactly in these steps:

1. ng new pwo-countup-library --create-application=false
2. ng g library countup
3. ng g application countup-test
4. ng build countup

5.imported CountupModule into app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { CountupModule } from 'countup';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule, CountupModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { } 
  1. added library component selector into app.component template

@Component({
selector: 'app-root',
template: `
  <lib-countup></lib-countup>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'countup-test';
}

countup.component looks like this:

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'lib-countup',
  template: `
    <p>
      countup works!
    </p>
  `,
  styles: []
})
export class CountupComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}

And here is countup.module:

import { NgModule } from '@angular/core';
import { CountupComponent } from './countup.component';



@NgModule({
  declarations: [CountupComponent],
  imports: [
  ],
  exports: [CountupComponent]
})
export class CountupModule { }

I get error in the app.component template:

lib-countup is not a known element

Can anybody help me? It's exactly the same problem like in this question https://stackoverflow.com/questions/62315080/selector-is-not-a-known-element-angular-9-custom-library[1] but so far none knows the answer to this problem.Thank you!

1 Answers

You are trying to use component with selector 'lib-countup' which does not exist. You have to create component with such selector in your library and add it to library module's exports array

@NgModule({
  declarations: [YourComponent], 
  exports: [YourComponent] <--- here
  imports: [],
  providers: []
  })
export class CountupModule{ }
Related