javascript - Angular Web 组件 - @Input 未收到值

标签 javascript angular

我遇到了一个问题,我创建了 Angular Web 组件(或 Angular Web 元素),并且尝试通过 @Input() 获取传递到组件中的值。这个值只是没有通过,但我已经让它与 Angular 中的任何其他 @Input() 相同。我很困惑。

代码如下所示:

组件.ts

@Input() displayGroup: string;

ngOnInit() {
   console.log(this.displayGroup);
}

使用元素

<my-custom-element displayGroup="test"></my-custom-element>

为什么输入的值会这样?控制台中显示的值为: 未定义

最佳答案

Angular Web 组件的工作方式与普通 Angular 组件略有不同。

从Angular的官方文档中可以看到here

The creation API parses the component looking for input properties, and defines corresponding attributes for the custom element. It transforms the property names to make them compatible with custom elements, which do not recognize case distinctions. The resulting attribute names use dash-separated lowercase. For example, for a component with @Input('myInputProp') inputProp, the corresponding custom element defines an attribute my-input-prop.

这意味着在 HTML 中您需要以下内容:

<my-custom-element display-group="test"></my-custom-element>

您声明的任何 @Input() 变量对于您将使用它的输入都需要是短横线小写

关于javascript - Angular Web 组件 - @Input 未收到值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62322167/

相关文章:

angular - 如何将不存在的链接重定向到 Angular 2 中的主页?

angular - 如何以我自己的方式设计 Angular 10 Material Select(和其他)的样式

javascript - 如何在类上但仅在某些元素上使用 onclick

javascript - 了解 Passport.authenticate ('local' ) 语法位

javascript - 使用 JS 循环创建 Bootstrap 网格

angular - 如何使用日期选择器在 Angular Material 5.0.0 中选择日期范围?

javascript - Angular 7 : How to Use croppie or any other cropper

mongodb - 在 Angular 4 中使用 mongodb-stitch 库

javascript - 滚动两个具有停靠效果的 div

javascript - 在 Jquery 中创建一个无限循环