1

我使用 ng-packagr 从 Angular 5 项目创建了 npm 包,现在我正尝试在新项目中安装该包。我在新项目的 NgModule 中包含包并像这样使用它:

  <created-package 
    [input1]="'value1'"
    [input2]="'value2'"  
  >
  </created-package>

该组件呈现,html 和包含的 css 显示,但 @Input 字段(输入 1 和输入 2)未定义。

这是我的 component.ts 的示例:

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

@Component({
  selector: 'created-package',
  templateUrl: './created-package.component.html'
})

export class CreatedPackage implements OnInit {
  @Input() value1: any;
  @Input() value2: any;


  constructor(private router: Router) {

  }

  ngOnInit() {
    console.log(this.value1, this.value2);

  }
}

它 console.logs 未定义..

如果有人知道答案,我将非常感谢您的回复!谢谢!

4

1 回答 1

1

您在组件内的输入变量应该具有与 html 中传递的名称匹配的名称,或者您可以将别名传递给@Input()

要么改变:

 <created-package 
    [value1]="'value1'"
    [value2]="'value2'"  
 >
 </created-package>

或者:

@Input('input1') value1: any;
@Input('input2') value2: any;
于 2018-08-10T14:03:36.323 回答