简体   繁体   English

无法在组件之间传递数据

[英]Unable to pass data between components

We are trying to pass the array data from the Parent component to the Child and it is throwing TypeError: Cannot read property 'setData' of undefined > Below is what we are trying 我们正在尝试将数组数据从父组件传递给子组件,并抛出TypeError: Cannot read property 'setData' of undefined >下面是我们正在尝试的

Parent Component 父组件

export class RFComponent implements DoCheck {

  finalResult: SModel[];

  @ViewChild(TSFComponent, {static: false})
  private childVar: TSFComponent;

  ........
  click() {
  this.elem = this.getResult(this.prj);
  this.childVar.setData(this.finalResult);
  }

Child Component 子组件

export class TSFComponent implements DoCheck {

  @Input() setData(e: SModel[]) {
    this.tjlData = e;
  }

I am not sure why we get Cannot read property error we call setData() from the parent 我不确定为什么会收到Cannot read property错误,因此从父级调用setData()

Please recheck your RFComponent . 请重新检查您的RFComponent There you will see that this.finalResult is not defined anywhere. 在那里,您会看到this.finalResult没有在任何地方定义。 You need to define it as member variable, like for example 您需要将其定义为成员变量,例如

private finalResult: string = 'something'

then you will be able to pass it to the other component. 那么您就可以将其传递给其他组件。

But also, this is not the best practice to do. 但这也不是最佳做法。 As far as I see here you did not define your components correctly. 据我所知,您没有正确定义组件。 Even though you can have pure classes in Angular, usually components have a decorator over the class declaration, where you define your selector, templateUrl and styles, eg 即使您可以在Angular中使用纯类,但通常组件在类声明中都有装饰器,您可以在其中定义选择器,templateUrl和样式,例如

@Component({
    selector: 'some-selector',
    templateUrl: 'my-template.component.html',
    styleUrls: ['my-style.component.scss']
})

There, in the template you can then reference the other class and pass data over @Input() , @Output() parameters. 在那里,您可以在模板中引用其他类,然后通过@Input()@Output()参数传递数据。

You don't need to define setData() on the child as an Input(). 您无需在子级上将setData()定义为Input()。 You are creating a reference to the child component when you use viewchild and therefore you can access the methods on it. 使用viewchild时,您正在创建对子组件的引用,因此可以访问其上的方法。 You are not passing the method setData from parent to child. 您没有将setData方法从父级传递给子级。

export class TSFComponent implements DoCheck {

  setData(e: SModel[]) {
    this.tjlData = e;
  }

You also need to give reference to the dom element in view child. 您还需要在view child中引用dom元素。

<child #TSFComponent></child>

@ViewChild('TSFComponent', {static: false}) childVar: TSFComponent;

As far as I see, you do not need the Input() decorator, and it should work if you remove it. 据我所知,您不需要Input()装饰器,并且如果删除它,它应该可以工作。 Precondition is, as already mentioned, that the TSFComponent is actually used inside the template of RFComponent. 如前所述,前提条件是TSFComponent实际上是在RFComponent模板内使用的。

You're using @ViewChild wrong. 您使用@ViewChild错误。 You don't need to use the setData() function to essentially 'set' the data. 您基本上不需要使用setData()函数来“设置”数据。 If you use the @Input() decorator in the Child component, you can set the and declare the variable there with the data passed down from the Parent Component. 如果在子组件中使用@Input()装饰器,则可以设置并在其中声明变量,并使用从父组件传递来的数据。

You can do it like this: 您可以这样做:

Parent Component HTML: 父组件HTML:

<child-component [parentData]="data"></child-component>

Child Component .ts 子组件.ts

@Input data: any;

Then, if you want to set a variable or object equal to the Child Component variable/object, then reference the child component variable in the parent component and assign the value. 然后,如果要设置与子组件变量/对象相等的变量或对象,则在父组件中引用子组件变量并分配值。 See below: 见下文:

Child component object -> Parent component object 子组件对象->父组件对象

Child Component giving the data object a new value 给数据对象赋予新值的子组件

someCode() {
    data = {
        someData: "Hi there",
        someMoreData: "Hello there again"
    }

Parent component referencing and assining a new object 父组件引用和关联新对象

parentData: any; 

assignData {
    this.childComponent.data = parentData;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM