属性 'downloadURL' 在类型 'AngularFireUploadTask' 上不存在

Property 'downloadURL' does not exist on type 'AngularFireUploadTask'

我的线路有问题

this.downloadURL = task.downloadURL()

即使我导入了 AngularFireUploadTask。

    import { Component, OnInit } from '@angular/core';
    import { AuthService } from '../../core/auth.service';
    import { AngularFireStorage, AngularFireStorageReference, AngularFireUploadTask } from 'angularfire2/storage';

    import { PostService } from '../post.service';
    import { Observable } from 'rxjs/Observable';



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

      title: string;
      image: string = null;
      content: string;

      buttonText: string = "Create Post"

      uploadPercent: Observable<number>
      downloadURL: Observable<string>

      constructor(
        private auth: AuthService,
        private postService: PostService, 
        private storage: AngularFireStorage
      ) { }

      ngOnInit() {
      }

      uploadImage(event) {
        const file = event.target.files[0]
        const path = `posts/${file.name}`
        if (file.type.split('/')[0] !== 'image') {
          return alert('only image files')
        } else {
          const task = this.storage.upload(path, file)

          this.downloadURL = task.downloadURL()

          this.uploadPercent = task.percentageChanges()
          console.log('Image Uploaded!')
          this.downloadURL.subscribe(url => this.image = url)
        }
      }

The message is:"Property 'downloadURL' does not exist on type 'AngularFireUploadTask'.".

我应该怎么做才能没有这个问题。

您要调用的方法是

getDownloadURL();

请查看此页面。

https://github.com/angular/angularfire2/blob/master/docs/storage/storage.md

在这里你可以看到方法的签名是

getDownloadURL(): Observable<any>
const task = this.storage.upload(path, file);
const ref = this.storage.ref(path);
this.uploadPercent = task.percentageChanges();
console.log('Image uploaded!');
task.snapshotChanges().pipe(
finalize(() => {
  this.downloadURL = ref.getDownloadURL()
  this.downloadURL.subscribe(url => (this.image = url));
})
)
.subscribe();

您需要从 this video 开始对代码进行实际更改。

更新 - 8/30/20

对于那些想要更简洁代码的人,请使用 promises(在异步函数中等待):

const task = this.storage.upload(path, file);
const ref = this.storage.ref(path);
this.uploadPercent = task.percentageChanges();

// upload image, save url
await task;
console.log('Image uploaded!');
this.image = await ref.getDownloadURL().toPromise();

我在使用 angular + firebase 上传图片时遇到这个错误,方法 DownloadURL() 不再依赖任务,所以我会 post 下面我是如何解决的那个错误

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.task.snapshotChanges().pipe(
  finalize(() => this.downloadURL = this.ref.getDownloadURL() ))
.subscribe();

如果你想要图片来源url任何东西,你可以这样获取,

 this.task.snapshotChanges().pipe(
 finalize(() => {
  this.ref.getDownloadURL().subscribe(url => {
    console.log(url); // <-- do what ever you want with the url..
  });
}))
.subscribe();