簡體   English   中英

角度2:未在父級上定義ViewChild

[英]Angular 2 : ViewChild is undefined on parent

我有以下錯誤:

錯誤:未捕獲(承諾):TypeError:無法設置未定義的屬性“ test_id” TypeError:無法設置未定義的屬性“ test_id”

錯誤在此行上觸發:

    console.log('after view init testList', this.productList.test_id);

我看到了很多帖子,但是它們似乎都已過時,並且大多數都在說我必須使用函數ngAfterViewInit。 我有一個觸發updateTestId的動作,我想將此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)

  }


}

這是我的孩子部分:

    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; }
*/

}

我像這樣使用子選擇器:

<test-list></test-list>
<product-list #productList [test_id]="selected_test"></product-list>

模板引用變量名稱添加到模板中的product-list

<product-list #productList [test_id]="selected_test"></product-list>

並在組件中通過引用

@ViewChild('productList')
private productList: ProductListComponent;

編輯:

在這種情況下, Vivek Doshi的回答是正確的,因為您是通過@Input將數據傳遞給孩子的,所以您不需要它。 但是仍然-如果您想使用ViewChild ,這是一個解決方案:)

如果您想將數據從父母傳遞給孩子,則可以直接傳遞

<product-list [test_id]="selected_test"></product-list>

就是這樣,僅此而已。

無需通過@ViewChild訪問它


從產品列表組件檢測隨時間變化的值

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

范例:

@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnChanges {
  @Input()
  prop: number;

  ngOnChanges(changes: SimpleChanges) {
    // changes.prop contains the old and the new value...
  }
}

注意事項:您需要在子組件上implements OnChanges

有關更多詳細信息,請閱讀此文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM