简体   繁体   English

将值从子组件传递到父组件时无法读取“目标”属性

[英]Cannot read “target” property when passing value from child to parent component

In my project student is child component of the parent app component.在我的项目中, student是父app组件的子组件。

The student component template has an input element as below: student组件template有一个输入元素,如下所示:

<input type='text' #inputbox (keyup)='onkeyUp(inputbox.value)'>

The student component has defined onkeyUp event handler as below: student组件定义onkeyUp事件处理程序,如下所示:

  @Component({
  outputs: ['childevent']
  ... 
  childevent = new EventEmitter();
  onkeyUp(value: any) {
  this.childevent.emit(value);
  }

The parent app component looks as below:app组件如下所示:

...
<label>Value of Child Component: </label> {{Cdata.target.value}}
<app-student (childevent)='Cdata=$event'></app-student>

Here, {{Cdata.target.value}} is throwing an error:在这里, {{Cdata.target.value}}抛出错误:

ERROR TypeError: Cannot read property 'target' of undefined

As per my understanding, $event has the entire childevent payload which is passed to the Cdata event handler.据我了解, $event具有传递给Cdata事件处理程序的整个childevent有效负载。 And, event.target.value returns the current contents of childevent并且, event.target.value返回childevent的当前内容

Please let me know if I have correct understanding.如果我有正确的理解,请告诉我。 I am referring to angular docs我指的是angular 文档

I have tried to use optional chaining我尝试使用optional chaining

<label>Value of Child Component: </label> {{Cdata?.target.value}}

But still getting an error但是还是报错

ERROR TypeError: Cannot read property 'value' of undefined

The problem is that in your app template , Cdata does not yet exist when the template is rendered for the first time.问题是在您的应用程序template中,第一次呈现模板时, Cdata还不存在。 You can solve this by changing the code as follows:您可以通过如下更改代码来解决此问题:

<label>Value of Child Component: </label> {{ Cdata ? Cdata.target.value : 'missing' }}

I think you just need to change the definition of the EventEmitter , you can check :我认为您只需要更改EventEmitter的定义,您可以检查

@Output() childevent = new EventEmitter();

暂无
暂无

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

相关问题 'TypeError: Cannot read property of x of undefined' 将数据从父组件传递到子组件但其他组件正在工作 - 'TypeError: Cannot read property of x of undefined' when passing data from parent to child component but others are working 将属性从父组件传递给子组件 - Passing a property from a parent to a child component TypeError: Cannot read property 'map' of undefined 当我尝试 map 通过从父组件传递到子组件的道具时显示 - TypeError: Cannot read property 'map' of undefined is showing when I'm trying to map over the props passed from parent component to child component 单击子组件时更新父组件的 state,返回错误“无法读取属性...” - Updating state of parent component when a child component is clicked, returns an error saying “cannot read property…” 使用window.open将数据从父级传递到子级时,为什么会显示“无法读取未定义的属性”元素”? - When passing data from parent to child with window.open, why do I get 'Cannot read property 'elements' of undefined'? TypeError:无法将子组件中未定义的属性“setState”读取到父组件 - TypeError: Cannot read property 'setState' of undefined in child to parent component 将 axios 获取的道具从父级传递给子级返回“类型错误:无法读取未定义的属性 &#39;x&#39;” - Passing axios fetched prop from Parent to Child returns “TypeError: Cannot read property 'x' of undefined” TypeError:无法读取未定义的属性“ stateUpdate”(哪个函数)/将数据从父级传递到子级 - TypeError: Cannot read property 'stateUpdate' (Which is a function) of undefined / Passing data from Parent to child 当事件在父组件中触发时,将值从 React 子组件传递给父组件? - Passing value from React child to parent component, when event is triggered in parent? 将函数从父级传递到子级组件的Input()属性时丢失上下文 - Losing context when passing function from parent to child component's Input() property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM