[英]How to assign data to variable in component?
I have core component: 我有核心组件:
export class AppComponent implements OnInit {
constructor() {
const toast: any = {
type: exception.type,
body: ToasterRenderComponent.messages = ["44"],
bodyOutputType: BodyOutputType.Component
};
this.toasterService.pop(toast);
}
}
In constructor I try to fill object toast
and assign data to variable of component ToasterRenderComponent
: 在构造函数中,我尝试填充对象
toast
并将数据分配给组件ToasterRenderComponent
变量:
ToasterRenderComponent.messages = ["44"]
Component ToasterRenderComponent
has public property messages
, but it does not work for me. 组件
ToasterRenderComponent
具有公共属性messages
,但它对我不起作用。
try this 尝试这个
import { Router } from '@angular/router';
export class AppComponent implements OnInit {
constructor(private router: Router) {
const toast: any = {
type: exception.type,
body: ToasterRenderComponent.messages = ["44"],
bodyOutputType: BodyOutputType.Component
};
this.router.config.find(r => r.component== ToasterRenderComponent).data = toast;
}
}
in component "ToasterRenderComponent" 在组件“ToasterRenderComponent”中
import { ActivatedRoute } from '@angular/router';
export class ToasterRenderComponent implements OnInit {
toasterObject: any;
constructor(private router: ActivatedRoute) { }
ngOnInit() {
this.router.data.subscribe(r=>this.toasterObject=r);
}
}
When working with cross component data transmission there are a on the top of my head two primary ways to do it depending on the relationship between the components. 在处理跨组件数据传输时,根据组件之间的关系,有两种主要方法可以实现。
If the components views are not nested (not parent and child), then the natural approach would be to create an Angular service . 如果组件视图不是嵌套的(不是父级和子级),那么自然的方法是创建一个Angular 服务 。
If the components are nested, one would usually pass data through template data binding . 如果组件是嵌套的,则通常会通过模板数据绑定传递数据。
Of course there are work-arounds, however to ensure that Angular destroys components correctly I'd suggest sticking to what the documentation states. 当然有一些解决办法,但要确保Angular正确地破坏组件,我建议坚持文档所说的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.