【问题标题】:"Property '...' doesn't exist on type FirebaseObjectObservable<any>" error when working with data received with @Input处理使用@Input 接收的数据时出现“FirebaseObjectObservable<any> 类型上不存在属性'...'”错误
【发布时间】:2017-02-04 22:58:34
【问题描述】:

我有一大块数据通过@Input 那个console.logs 传递给一个子组件,但是当我去处理这些数据时,它会抛出一个错误,说我试图访问的对象不存在输入FirebaseObjectObservable&lt;any&gt;

这是数据块的结构

"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 个问题。 if 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 并得到相同的错误。我看到的每个示例几乎都是单层数据,因此很难判断我在检索嵌套数据体方面做错了什么。有人可以帮忙吗?

【问题讨论】:

    标签: angular firebase firebase-realtime-database angular2-inputs


    【解决方案1】:

    我认为您最好使用对象而不是可观察对象来保存数据。但即使在这种情况下,您也会收到错误消息,因为您必须预定义接口以匹配您将要接收的数据。如果您没有它,您可以像这样访问该属性:this.questions['question01'];

    【讨论】:

      猜你喜欢
      • 2017-05-23
      • 2017-02-10
      • 2021-06-21
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 2021-02-08
      相关资源
      最近更新 更多