Angular 2:ViewChild 在父级上未定义

Nic*_*s D 0 javascript angular

我有以下错误:

错误:未捕获(承诺):类型错误:无法设置未定义的属性“test_id”类型错误:无法设置未定义的属性“test_id”

错误是在这一行上触发的:

    console.log('after view init testList', this.productList.test_id);
Run Code Online (Sandbox Code Playgroud)

我看到了很多帖子,但所有帖子似乎都过时了,而且大多数帖子都说我必须使用我使用的函数 ngAfterViewInit。我有一个触发 updateTestId 的 clic 操作,我想将此 ID 传递给我的子视图 ProductListComponent。这是我的父组件:

import { Component,ViewChild} from '@angular/core';

import {Test,TestData} from './testService';

import { LocalDataSource } from 'ng2-smart-table';
import {ProductListComponent} from '../../components/product/list/productList.component';



@Component({
  selector: 'test-list',
  templateUrl: './testList.html',
  styleUrls: ['./testList.scss']
})
export class TestListComponent{

  //tests: Test[];
  tests: any[];
  selected_test : number;


  @ViewChild(ProductListComponent)
  private productList: ProductListComponent;

  constructor(protected service: TestData){
    this.service.getData().then((data) => {
      this.tests = data.tests;
      this.source.load(data);
    });


  }

  settings = {
    editable : false,
    actions: {
      add:false,
      edit:false,
      delete:false
  },
  columns: {
      id: {
        title: 'ID',
        type: 'number'
      },
      nb_cartons: {
        title: 'Cartons',
        type: 'number'
      },
      nb_items: {
        title: 'Items',
        type: 'number'
      },
      nb_pallets: {
        title: 'Pallets',
        type: 'number'
      },
  };

  //source : Test[];
  source: LocalDataSource = new LocalDataSource();


  public updateTestId(value:any):void {

    this.selected_test = value.data.id;
    console.log('rowSelect', this.selected_test );
    //console.log(this.productList.test_id)

  }


}
Run Code Online (Sandbox Code Playgroud)

这是我的子组件:

    import { Component,Input,Output, OnChanges, SimpleChanges } from '@angular/core';

import { LocalDataSource } from 'ng2-smart-table';
import {Test} from '../../test/testService';

@Component({
  selector: 'product-list',
  templateUrl: './productList.html',
  styleUrls: ['./productList.scss']
})
export class ProductListComponent implements OnChanges{

  @Input() test_id : number = null;

  settings = {
    editable : false,
    actions: {
      add:false,
      edit:false,
      delete:false
    },
    columns: {
      id: {
        title: 'ID',
        type: 'number'
      },
      sku: {
        title: 'SKU',
        type: 'string'
      },
      reference: {
        title: 'Reference',
        type: 'string'
      },
      size: {
        title: 'Size',
        type: 'string'
      },
    },
    edit: {
      editButtonContent: '<i class="ion-edit"></i>',
      saveButtonContent: '<i class="ion-checkmark"></i>',
      cancelButtonContent: '<i class="ion-close"></i>',
    }
  };

  source: LocalDataSource = new LocalDataSource();


  constructor() {

  }

  ngOnChanges(changes: SimpleChanges) {
    console.log(changes['test_id'],changes['test_id'].currentValue);
    console.log('change in child',changes.test_id);
  }

  /*
  @Input()
  set _test_id(id: number) {
    this._test_id = id;
  }

  get test_id(): number { return this._test_id; }
*/

}
Run Code Online (Sandbox Code Playgroud)

我像这样使用子选择器:

<test-list></test-list>
<product-list #productList [test_id]="selected_test"></product-list>
Run Code Online (Sandbox Code Playgroud)

Fre*_*din 5

添加一个模板引用变量product-list模板

<product-list #productList [test_id]="selected_test"></product-list>
Run Code Online (Sandbox Code Playgroud)

并在组件中引用它

@ViewChild('productList')
private productList: ProductListComponent;
Run Code Online (Sandbox Code Playgroud)

编辑:

在这种情况下,Vivek Doshi在他的回答中是正确的,您不需要它,因为您通过@Input. 但是 - 如果你想使用ViewChild,这是一个解决方案:)