简体   繁体   English

组件的@Input突然产生错误

[英]Component's @Input suddenly produces errors

So i have this ultra weird scenario... 所以我有一个非常奇怪的场景...

I left my application working perfectly fine a week ago and got back to work on it today, realizing the dropdown i have doesn't work anymore... 一周前,我离开了我的应用程序,一切正常,今天又恢复工作,意识到我的下拉列表不再起作用了...

I have the super simple Component: 我有超级简单的组件:

import {Component, Input} from '@angular/core';
import {Card} from "../../entities/Card";

@Component({
    selector: 'card-details',
    templateUrl: './app/components/card-details/CardDetails.html',
})
export class CardDetailsComponent {
    @Input() card: Card;

    constructor() {}
}

HTML: HTML:

<template #cardDetails>
    <label>{{card.cardNumber}}</label>
    <br/>
    <label>{{card.cardNetwork}}</label>
    <br/>
    <label>{{card.type}}</label>
</template>
<button class="dropdown-item" placement="right" [ngbTooltip]="cardDetails">
    {{card.cardName}}
</button>

And the Parent Component's relevant html code: 以及父组件的相关html代码:

<div ngbDropdown class="d-inline-block">
    <button class="btn btn-outline-primary" id="dropdownMenu" ngbDropdownToggle>Choose a card..</button>

    <div class="dropdown-menu" aria-labelledby="dropdownMenu">
        <card-details *ngFor="let card of cards; let i = index" [card]="card" (click)="selectCard(i)"></card-details>
    </div>
</div>

Currently I'm receiving an error saying it couldn't read the card's properties, cannot read property cardName of undefined . 目前,我收到一条错误消息,提示它无法读取卡的属性, cannot read property cardName of undefined

Feels like i have to mention it again, I changed literally -nothing- in my code, the dropdown worked perfectly fine displaying all the details, just ran it again after a week. 感觉就像我不得不再次提到它,我在代码中进行了任何更改,但实际上没有做任何更改,下拉菜单在显示所有详细信息的情况下工作得非常好,一周后就再次运行了。

Any ideas? 有任何想法吗?

Thanks in advance for all the help, let me know if something is missing 在此先感谢您提供的所有帮助,如果有任何遗漏,请通知我

EDIT 编辑

Might be worth noting that placing a console.log(JSON.stringify(this.card) on the ngOnInit it has the card filled with the data. So it is filled with the correct data from the DB but still produces this error. 可能值得注意的是,将console.log(JSON.stringify(this.card)放置在ngOnInit时,该卡中已填充了数据。因此,它已填充了来自数据库的正确数据,但仍会产生此错误。

Edit2 编辑2

Moreover, inspecting the HTML Elements I can see that there are exactly two card-details containing their correct names... so what is the problem here? 此外,检查HTML元素,我可以看到恰好有两个包含其正确名称的card-details ...所以这里出了什么问题? :O :O

UPDATE 更新

Again, cannot find any logic in that -> i initialized it with a: 再一次,在那找不到任何逻辑->我用以下命令初始化了它:

let defaultCard: Card = new Card();
defaultCard.cardName = "default";
defaultCard.cardNetwork = "whatever";
defaultCard.cardNumber = 1564987;
defaultCard.type = "Debit";

and now the logs within the ctor and ngOnInit : 现在是ctorngOnInit的日志:

ctor
{"cardName":"default","cardNetwork":"whatever","cardNumber":1564987,"type":"Debit"}
ctor
{"cardName":"default","cardNetwork":"whatever","cardNumber":1564987,"type":"Debit"}
init
{"cardNumber":208461269,"cardName":"Magic Card","cardNetwork":"American Express","type":"Credit"}
init
{"cardNumber":220319960,"cardName":"Men Card","cardNetwork":"Visa","type":"Debit"}

Please, any insights would help me a lot! 拜托,任何见解都会对我有很大帮助!

You're looping through the cards list. 您正在浏览cards列表。 If you didn't change anything in your code and it suddenly starts to fail - something probably changed your data. 如果您没有更改代码中的任何内容而突然开始失败-则可能是某些内容更改了您的数据。 Check your database? 检查您的数据库? or where you read your cards from - they're probably reading empty cards. 或您从中读取卡片的位置-他们可能正在读取空卡片。

Make sure where the error originates from where you think it does. 确保错误从何而来。 Debugging the code that we can not see will help you pinpoint the error. 调试我们看不到的代码将帮助您查明错误。

Can you try referencing card?.cardName instead? 您可以尝试引用card?.cardName吗? The component might be getting undefined for a fraction of a second. 该组件可能在不到一秒钟的时间内变得不确定。 If that works (or not) it might points us in the right direction? 如果这行得通(或行不通),它可能会为我们指明正确的方向?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM