簡體   English   中英

FirebaseObjectObservable類型上不存在“屬性&#39;...&#39; <any> 使用@Input接收的數據時發生錯誤”

[英]“Property '…' doesn't exist on type FirebaseObjectObservable<any>” error when working with data received with @Input

我有大量數據通過console.logs的@Input傳遞給子組件,但是當我使用該數據時,它拋出一個錯誤,指出我嘗試訪問的對象在FirebaseObjectObservable<any>類型上不存在FirebaseObjectObservable<any>

這是數據塊的結構方式

"questions" : {
        "question01"    : {
            "id"        : "an ID",
            "name"      : "a name",
            "question"  : "a question",
            "answers"   : {
                "answer01"  : {
                    "answer"    : "some answer",
                    "id"        : "an ID"
                },
                "answer02"  : {
                    "answer"    : "another answer",
                    "id"        : "an ID"
                }
            }
        },
        "question02"    : {....},
        "question03"    : {....}
    }

在子組件內部按此方式交付

@Input('busImageQuesI')
questions:  FirebaseObjectObservable<any>; // console.logs the questions showing they made it safely to the component.

該子組件將在其他父組件中使用,並根據要調用的父組件填充自己的子組件。到目前為止,我認為我會使用if語句來確定顯示哪些數據集。 到目前為止看起來像這樣

expbTrigger: boolean =  false;
rebbTrigger: boolean =  false;
newbTrigger: boolean =  false;

ngOnInit() {

    var myLocation = window.location.pathname;


    if (myLocation.includes('expand')){
        this.expbTrigger = true;

    } else if (myLocation.includes('recreate')){
        this.rebbTrigger = true;

    } else if (myLocation.includes('new-business')){
        this.newbTrigger = true;
    }

    console.log(this.expbTrigger, this.rebbTrigger, this.newbTrigger);
}

我正在使用它來切換模板中的其他元素

<multiple-choice-radio *ngIf="expbTrigger"></multiple-choice-radio>
<multiple-choice-radio *ngIf="rebbTrigger"></multiple-choice-radio>
<multiple-choice-radio *ngIf="newbTrigger"></multiple-choice-radio>

我這樣做是基於父組件發送我想要的問題,因為它們都不是僅一部分獨占的,但是它們並沒有全部使用,因此我想我將手動設置它們並使用*ngIf在適當的時候打開和關閉它們。

@Input接收的questions對象中包含3個問題。 如果expbTrigger = true我要使用問題1和2。如果rebbTrigger = true我要使用問題3。我認為這段代碼可以工作

questionA:  FirebaseObjectObservable<any>;
questionB:  FirebaseObjectObservable<any>;

ngOnChanges(){
    if(this.expbTrigger = true){
        this.questionA = this.questions.question01;
        this.questionB = this.questions.question02;
    } else if(this.rebbTrigger = true){
        this.questionA = this.questions.question03;
        this.questionB = null;
    }
}

但是我得到這個錯誤

Property 'question01' does not exist on type 'FirebaseObjectObservable<any>'

它對問題2和3表示相同。當我注釋掉代碼並執行console.log(questions)它將記錄並顯示其中的所有問題。 我也嘗試將其移動到ngAfterViewInit ,並得到了相同的錯誤。 我看到的每個示例幾乎都是1個數據層,因此很難說出我在檢索嵌套數據體方面做錯了什么。 有人可以幫忙嗎?

我認為您最好使用對象而不是可觀察對象來保留數據。 但是即使在這種情況下,您也會收到錯誤消息,因為您必須預先定義接口以匹配要接收的數據。 如果沒有它,則可以這樣訪問屬性: this.questions['question01'];

暫無
暫無

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

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