Angular 元素 : @Input decorator not working with variables

标签 angular custom-element angular-elements

我有几个独立的 Web 组件,由 angular 元素组成,我将它们导入到一个主组件中,该组件有一个路由器并且是基本应用程序。
路线非常简单:

import { Component } from '@angular/core';
import { Router } from "@angular/router";

@Component({
    selector: 'example',
    template: '<component-one [variable]="toPass" (callback)="onCallback($event)"></component-one>',
    styles: []
})
export class ExampleComponent {
    public toPass: string = "Hello World!";

    constructor(private router: Router) {}

    onCallback(event) {
        console.log(event);
        this.router.navigate(['example-two']);
    }
}

组件做他们应该做的事情。
import { Component, Input, OnInit } from '@angular/core';

@Component({
    selector: "component-one",
    templateUrl: "./component-one.html",
    styleUrls: ["./component-one.scss"],
    encapsulation: ViewEncapsulation.Emulated,
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class ComponentOne implements OnInit {
    @Input() variable: string;
    @Output() callback = new EventEmitter<any>();

    constructor() {}

    ngOnInit() {
        console.log("Variable: ", this.variable);
    }

    someRandomButtonClicked() {
        callback.emit({ data: "Callback Called" });
    }
}

现在,当我启动主应用程序时,一切都按预期显示,回调工作正常,但变量未定义。
我在 webcomponents 的 @Input 声明中遗漏了什么吗?

最佳答案

由于您使用的是 Angular 元素,请确保您的变量名称在 中。小型股像这样@Input() myvariable: string而不是 @Input() myVariable: string 在这里检查
https://github.com/angular/angular/issues/24871

关于 Angular 元素 : @Input decorator not working with variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52540889/

相关文章:

javascript - 组件构造函数被 Angular 元素调用两次

angular - 如何在 watch 模式下提供 Angular Elements 应用程序?

浏览器中缺少 Angular 12 源映射

javascript - 通过将数据存储在类变量中来间接更改 UI 不起作用

dart - 找不到自定义元素

angular - 在 webcomponent 中重置 Angular 路由器状态( Angular 元素)

Angular 2 : Pass a function to the ngClass tag on the template

angular - 使用 d3-brush 时鼠标按下时出错 : Cannot read property 'touches' of null

javascript - 正在申请:focus CSS to input-field in shadowDOM and :focus CSS to shadow host

javascript - 我应该如何在 Angular 自定义元素(Web 组件)中引用 Assets