Angular 库在使用时不是 运行 ngOnInit

Angular Library is not running ngOnInit when used

我正在按照 Angular CLI 6 说明创建库 here。我能够创建我的库,并成功构建它。该库由我用于 UI 的 Component 和 HTML selector.

组成
@Component({
  selector: 'my-lib',
  templateUrl: './my-lib.component.html',
  styleUrls: ['./my-lib.component.css'],
  providers: [MyService],
})

export class MyComponent implements OnInit {
  @Input() id: string;

  constructor(
    private myService: MyService) {
    console.log("constructor");    
  }

  ngOnInit {
    console.log("ngOnInit");
    this.myService.DoStuff();
  }
}

现在我有一个单独的项目,我想在其中使用这个库。我还没有发布它,所以我通过将 dist/<my-lib> 的内容复制到 node_modules 中来使用它另一个项目。我之前使用 npm link,但我遇到了 here 描述的问题,这个解决方案对我有用。

我可以成功导入库和 Module 并使用 HTML 文件中的 Component 及其选择器,同时传递输入:<my-lib [id]="'my-id'">id 被正确传递,但是 ngOnInit 中的逻辑没有得到 运行。控制台不打印 "ngOnInit"。但它确实打印 "constructor",并且组件 UI 元素正确显示。

我是不是遗漏了将 ngOnInit 逻辑转换为 运行 的内容?

编辑:我应该提到图书馆是 运行ning:

我尝试在其中使用该库的项目是:

不确定这是否会影响任何事情。

EDIT2:修复示例代码。

我看到的唯一问题是您的服务实例也同名,请将其更改为

constructor(
    private apiService: myService) {
    console.log("constructor");    
  }

ngOnInit {
    console.log("ngOnInit");
    this.apiService.DoStuff();
  }

也希望你导入了,

import { Component, OnInit } from '@angular/core';

同时删除组件中的提供程序并将其移动到模块级别。