无法通过Angular2-RC4中的View Child访问导入的组件

时间:2016-07-07 13:55:18

标签: typescript angular ionic2

这是我的代码,但我收到undefined错误:

import {IONIC_DIRECTIVES} from 'ionic-angular'

@Component({
    selector:       'personal-info', 
    templateUrl:    'build/pages/transaction/personal-info/personal-info.html',
    directives:     [IONIC_DIRECTIVES]
})

export class PersonalInfoPage {

    infoForm: ControlGroup

    constructor (
        private formBuilder: FormBuilder, 
    ) {
        this.infoForm = formBuilder.group({//...})
    })
}

import {PersonalInfoPage} from './personal-info/personal-info'

@Component({
    templateUrl:    'build/pages/transaction/transaction.html',
    directives:     [PersonalInfoPage,PaymentDetailsPage,AddressPage,IdentityPage],
})

export class TransactionPage implements AfterViewInit {

    @ViewChild('PersonalInfoPage')      personalInfo:   PersonalInfoPage
    @ViewChild('PaymentDetailsPage')    paymentDetails: PaymentDetailsPage
    @ViewChild('AddressPage')           address:        AddressPage
    @ViewChild('IdentityPage')          identityPage:   IdentityPage

    constructor(
    ) {}

    ngAfterViewInit () {
        console.log(this.personalInfo.infoForm.valid);
    }

浏览器出错:

  

EXCEPTION:错误:未捕获(在承诺中):EXCEPTION:错误   ./TransactionPage类TransactionPage_Host - 内联模板:0:0   原始异常:TypeError:无法读取属性' infoForm'的   未定义

我缺少什么想法?

1 个答案:

答案 0 :(得分:3)

删除'

@ViewChild(PersonalInfoPage) 

如果查询类型,请使用该类型。如果查询模板变量,请使用字符串。

@ViewChild(PersonalInfoPage)      personalInfo:   PersonalInfoPage
@ViewChild(PaymentDetailsPage)    paymentDetails: PaymentDetailsPage
@ViewChild(AddressPage)           address:        AddressPage
@ViewChild(IdentityPage)          identityPage:   IdentityPage