首页 > 解决方案 > 'AngularFireUploadTask' 类型上不存在'downloadURL'

问题描述

我的线路有问题

this.downloadURL = this.task.downloadURL();

即使我导入了 AngularFireUploadTask。

    import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Router } from '@angular/router'
import { AngularFireStorage, AngularFireStorageReference, AngularFireUploadTask } from 'angularfire2/storage'
import { Observable, of } from 'rxjs';

@Component({
  selector: 'app-userprofile',
  templateUrl: './userprofile.component.html',
  styleUrls: ['./userprofile.component.css']
})
export class UserprofileComponent implements OnInit {

email: string;
uid: string;
id: any;
myid: string;
userKey: any;
data = {
    name : '',
    phone : '',
    age : '',
    address : '',
    city : '',
    job: '',
    email: ''
};
itemList: AngularFireList<any>
itemArray = []

ref: AngularFireStorageReference;
task: AngularFireUploadTask;
downloadURL :Observable<any>;


  constructor(private afStorage: AngularFireStorage, public db:AngularFireDatabase, public rout: Router) { 
    this.email = localStorage.getItem('email')
    this.myid = localStorage.getItem('uid')

    this.itemList = db.list('users')

    this.itemList.snapshotChanges()
    .subscribe(actions=>{
        actions.forEach(action=>{
            let y = action.payload.toJSON()
            y['$key'] = action.key
            this.itemArray.push(y as ListItemClass)
            if(action.payload.child('uid').val() == this.myid) {
          this.userKey = action.key
                this.itemArray.push(y as ListItemClass)
                this.data.name = this.itemArray[0]['name']
                this.data.phone = this.itemArray[0]['phone']
                this.data.age = this.itemArray[0]['age']
                this.data.address = this.itemArray[0]['address']
                this.data.city = this.itemArray[0]['city']
                this.data.job = this.itemArray[0]['job']
          this.data.email = this.itemArray[0]['email']

           console.log(this.userKey);
           console.log(this.itemArray)


            }
        })
    })

  }

  ngOnInit() {
    console.log(this.email)
    console.log(this.myid)
  }

  upload(event){
    const id = Math.random().toString(36).substring(2);
    this.ref = this.afStorage.ref(id);
    this.task = this.ref.put(event.target.files[0]);
    this.downloadURL = this.task.downloadURL();
  }

  onEdit(){
    this.itemList.set(this.myid , {
      name : this.data.name,
      phone : this.data.phone,
      age : this.data.age,
      address : this.data.address,
      city : this.data.city,
      job : this.data.job,
      email: this.email,
      uid: this.myid
    })
    this.rout.navigate(['/home']);
  }

}

export class ListItemClass {
      name : string;
    phone : string;
    age : string;
      address: string;
    city: string;
    job: string;
    email: string;
}

错误是:“AngularFireUploadTask”类型上不存在属性“downloadURL”。

标签: angulartypescriptangularfireangularfire2

解决方案


image: string = null
....
....
upload(event){
      const id = Math.random().toString(36).substring(2);
      this.ref = this.afStorage.ref(id);
      this.task = this.ref.put(event.target.files[0]);
      task.snapshotChanges().pipe(
        finalize(() => {
          this.downloadURL = ref.getDownloadURL()
          this.downloadURL.subscribe(url => (this.image = url));
        })
      )
      .subscribe();
}

有关更多信息,请查看


推荐阅读