角度库中的组件选择器不是已知元素

时间:2020-07-03 09:54:45

标签: angular angular-library

我完全按照以下步骤创建了简单的角度库:

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。将CountupModule导入到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. 将库组件选择器添加到app.component模板中

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

countup.component看起来像这样:

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

  constructor() { }

  ngOnInit(): void {
  }

}

这是countup.module:

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



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

我在app.component模板中遇到错误:

lib-countup不是已知元素

有人可以帮助我吗? 这与问题https://stackoverflow.com/questions/62315080/selector-is-not-a-known-element-angular-9-custom-library[1]中的问题完全相同,但是到目前为止,没有人知道这个问题的答案。谢谢!

1 个答案:

答案 0 :(得分:0)

您正尝试将组件与选择器'lib-countup'一起使用,该选择器不存在。您必须在库中使用此类选择器创建组件,然后将其添加到库模块的导出数组中

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