Angular 6 выдает ошибку «TypeError: Cannot read property» при вызове свойства из другого компонента при использовании модального - PullRequest
0 голосов
/ 13 ноября 2018

Я новичок в angular, поэтому прошу прощения, если я делаю глупые ошибки, когда пытаюсь вызвать текущее значение компонента A из компонента B. Это дает ошибку.я пытаюсь вызвать это значение, когда всплывающая модальная форма активна.при открытии всплывающего окна я пытался вызвать функцию editUserData (), но она выдает ошибку.

компонент A

import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import { userRegistrationData } from 
'src/app/models/registrationUser.models';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { EditUserDetailComponent } from '../editUser/edit-user-detail/edit- 
user-detail.component';
@Component({
selector: 'app-registration-form',
templateUrl: './registration-form.component.html',
styleUrls: ['./registration-form.component.css']
})
export class RegistrationFormComponent implements OnInit {

constructor(private httpClient: HttpClient ) { }


public currentValue: string;


editUserData(value:any): void
{
  this.currentValue = value;
}

}

компонент B

import { Component, OnInit,ViewChild, ViewChildren, AfterViewInit, 
ElementRef, HostListener, Input, Injectable, AfterViewChecked, 
AfterContentInit, ContentChild } from '@angular/core';
import {ModalDirective} from 'mdbootstrap';
import { userRegistrationData } from 
'src/app/models/registrationUser.models';
import { NgForm } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { RegistrationFormComponent } from '../../registration- 
form/registration-form.component';

@Component({
selector: 'app-edit-user-detail',
templateUrl: './edit-user-detail.component.html',
styleUrls: ['./edit-user-detail.component.css']
})

export class EditUserDetailComponent implements OnInit , AfterViewInit {
 public editThisDetail: userRegistrationData;
value : string;
constructor(private httpClient: HttpClient, ){}

@ViewChild('RegistrationFormComponent') registrationFormComponent: 
RegistrationFormComponent;


 @ContentChild(RegistrationFormComponent)
public registraionComponent :  RegistrationFormComponent;



  pushData()
  {
    console.log("asif");
    console.log("asif",this.registraionComponent.currentValue);
  }

  ngAfterViewInit()
  {
    this.pushData();
  }

  ngOnInit() {

  }

  }

ошибка В коде: RegistrationFormComponent.html: 81 ОШИБКА TypeError: Невозможно прочитать свойство 'currentValue' из неопределенного в EditUserDetailComponent.push ../ src / app / createUser / editUser / edit-user-detail / edit-user-detail.component.ts.EditUserDetailComponent.pushData (edit-user-detail.component.ts: 50) в EditUserDetailComponent.push ../ src / app / createUser / editUser / edit-user-detail / edit-user-detail.component.ts.EditUserDetailComponent.ngAfterViewInit

1 Ответ

0 голосов
/ 13 ноября 2018

В вашем случае EditUserDetailComponent дочерний и RegistrationFormComponent родительский, вы можете получить доступ через DI следующим образом -

EditUserDetailComponent

constructor(private httpClient: HttpClient, 
     @Inject(forwardRef(() => RegistrationFormComponent)) private registraionComponent:RegistrationFormComponent){

}

Удалить следующий код

@ViewChild('RegistrationFormComponent') registrationFormComponent: 
RegistrationFormComponent;


 @ContentChild(RegistrationFormComponent)
public registraionComponent :  RegistrationFormComponent;
...