简体   繁体   English

如何将数据分配给组件中的变量?

[英]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.

相关问题 如何通过从数据库(角度6和firebase)中检索数据的服务为组件变量赋值? - How to assign value to a component variable from a service retrieving data from a database (angular 6 and firebase)? 如何将 chrome.storage 中的数据分配给 Angular 组件的变量 - How to assign data from chrome.storage to Angular component's variable Angular Firestore - 获取文档数据并分配给组件内的变量 - Angular Firestore - Get document data and assign to variable inside the component 如何将Firestore数据字段分配给变量? - How to assign firestore data field to a variable? 如何在Angular组件[ngrx存储]中将状态值分配给字符串变量 - How to assign state value into string variable in Angural Component [ngrx Store] Angular + PIXI-如何将组件方法分配给变量并保留“ this”引用 - Angular + PIXI - How to assign a component method to a variable and preserve 'this' reference Angular:如何将所选单选按钮的值分配给.ts组件中的变量? - Angular: How to assign the value of the selected radio button to a variable in the .ts component? Angular4如何在确保父对象组件变量未定义之后从父组件变量中为其分配变量 - Angular4 how to assign variable in child component from parent component variable after reassuring that it is not undefined 将组件变量作为参数传递或将其分配为全局变量 - To pass a component variable as parameter or assign it as a global variable Angular Http 订阅 - 无法分配给组件中的变量 - Angular Http subscribe - cannot assign to variable in component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM