【问题标题】:Angular Property '$ref' is missing in type 'AngularFireList<{}>'AngularFireList<{}> 类型中缺少 Angular 属性'$ref'
【发布时间】:2018-06-13 13:14:30
【问题描述】:

我收到了这个错误

/Users/macmini/Desktop/newap/src/app/component/content/content.component.ts (18,5):类型“AngularFireList”不可分配给类型“FirebaseListObservable”。

“AngularFireList”类型中缺少属性“$ref”。

组件.ts

import { Component, OnInit } from '@angular/core';
import {Subscription} from 'rxjs';

import {AngularFireDatabase } from 'angularfire2/database';
import {FirebaseListObservable} from 'angularfire2/database-deprecated';

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

export class ContentComponent implements OnInit {

  courses$:FirebaseListObservable<any[]>;

  constructor(db:AngularFireDatabase){
    this.courses$ = db.list('/courses');

  }

  ngOnInit() {

  }

}

component.html

<ul *ngFor="let item of (courses$ | async)">
  <li><a href="#">{{item.$value}}</a>

  </li>
</ul>

应用模式

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {AngularFireModule} from 'angularfire2'
import {AngularFireDatabaseModule} from 'angularfire2/database'
import {environment} from '../environments/environment';

import { AppComponent } from './app.component';
import { ContentComponent } from './component/content/content.component';




@NgModule({
  declarations: [
    AppComponent,
    ContentComponent,

  ],
  imports: [
    BrowserModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireDatabaseModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

package.ison

{
  "name": "newap",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.0",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "angularfire2": "^5.0.0-rc.3",
    "core-js": "^2.4.1",
    "firebase": "^4.7.0",
    "rxjs": "^5.0.1",
    "xjs": "^0.1.6",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.1.0",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "@types/jasmine": "2.5.45",
    "@types/node": "~6.0.60",
    "angular2": "^2.0.0-beta.21",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }
}

我使用版本 angular 和 nodejs @角/cli:1.1.0 节点:8.11.2 操作系统:达尔文 x64

【问题讨论】:

    标签: javascript angular firebase angular2-services


    【解决方案1】:

    我认为问题与您使用不推荐使用的访问列表数据的方法有关。访问列表请参考this link for the relevant Angularfire2 documentation

    它提供了这个不使用FirebaseListObservable的例子:

    import { Component } from '@angular/core';
    import { AngularFireDatabase } from 'angularfire2/database';
    import { Observable } from 'rxjs';
    
    @Component({
      selector: 'app-root',
      template: `
      <ul>
        <li *ngFor="let item of items | async">
           {{ item | json }}
        </li>
      </ul>
      `,
    })
    export class AppComponent {
      items: Observable<any[]>;
    
      constructor(db: AngularFireDatabase) {
        this.items = db.list('items').valueChanges();
      }
    
    }
    

    【讨论】:

    • 我使用 angular2 版本:1.1.0 节点:8.11.2 操作系统:darwin x64
    • 这里重要的包是 Angularfire2 包……不是你的 Angular CLI 版本,不是你的 Node 版本,也不是你的操作系统。调整 TypeScript 和 HTML 文件中的代码,使其不使用 Angularfire2 的已弃用部分,并且您不应再收到该错误。
    • 感谢您的回答,感谢您的努力,
    • 我想看看这个问题,它也与 observable stackoverflow.com/questions/50841487/… 相关联
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2021-03-24
    • 2017-09-14
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2017-02-15
    相关资源
    最近更新 更多