Angular 将数据传递到角度Web组件
我正在尝试用Angular编写web组件,但我似乎不知道如何将数据传递到组件中Angular 将数据传递到角度Web组件,angular,web-component,Angular,Web Component,我正在尝试用Angular编写web组件,但我似乎不知道如何将数据传递到组件中 <my-component someId="1234"></my-component> export class MyComponent { @Input() someId: string = '1234';//default value } 我希望找到一些方法来实现这一点,并在我的角度分量中得到someId。是否可以实现此功能,或者我应该尝试使用插槽? 为了清楚
<my-component someId="1234"></my-component>
export class MyComponent {
@Input() someId: string = '1234';//default value
}
我希望找到一些方法来实现这一点,并在我的角度分量中得到someId。是否可以实现此功能,或者我应该尝试使用插槽?
为了清楚起见,我想问的是如何使用角度分量而不是正常的角度分量。你应该使用角度分量 您需要使用共享组件中声明的@Input()变量
<my-component someId="1234"></my-component>
export class MyComponent {
@Input() someId: string = '1234';//default value
}
//HTML
<my-component someId="1234"></my-component>
“someId”是一个可选的输入参数,也可以忽略
编辑:在将常量字符串或数字绑定到输入的情况下,不必在方括号中指定。您可以使用与普通html属性相同的语法。我有这个问题大约两天了,终于找到了解决方案 您需要做的是在组件子级(要公开的web组件)中使用方法ngOnChanges()。因为这是为了检测@Input()字段的任何更改 在这里,我留下了我的web组件的一段代码:
@组件({
选择器:'应用程序装运根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.scss']
})
导出类AppComponent实现OnInit、OnChanges{
@输入()域;
@Output()response=neweventemitter();
构造函数(){
}
ngOnInit():void{
}
ngOnChanges(更改:SimpleChanges):无效{
if(changes.domain){
log(“域URL:,this.Domain”);
this.response.emit(this.domain);
}
}
}
@Component({
selector: 'app-shipment-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnChanges {
@Input() domain;
@Output() response = new EventEmitter<string>();
constructor() {
}
ngOnInit(): void {
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.domain) {
console.log("Domain URL: ", this.domain);
this.response.emit(this.domain);
}
}
}