【发布时间】:2019-03-03 14:22:22
【问题描述】:
我有几个独立的 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 声明中是否缺少某些内容?
【问题讨论】:
-
您在all browsers 中看到相同的行为吗?在 Chrome 和 Firefox 中是一样的吗?
-
在 Opera、Firefox 和 IE 上相同,不支持 customElements。
标签: angular custom-element angular-elements