【问题标题】:Angular - error TS2339: Property 'take' does not exist on type 'AngularFireObject<{}>'Angular - 错误 TS2339:“AngularFireObject<{}>”类型上不存在属性“take”
【发布时间】:2019-04-27 19:24:06
【问题描述】:

我正在尝试编辑产品表单,但在尝试使用属性“take”时出现错误。 更具体地说,我得到:“类型 'AngularFireObject' 上不存在属性 'take'。”

import { Component, OnInit } from '@angular/core';
import { CategoryService } from 'src/app/category.service';
import { ProductService } from 'src/app/product.service';
import { Router, ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/take';

@Component({
  selector: 'app-product-form',
  templateUrl: './product-form.component.html',
  styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {
  categories$;
  product = {};
  //productService: any;

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    //private categoryService: CategoryService, 
    private productService: ProductService) { 
    //this.categories$ = categoryService.getCategories()


    let id = this.route.snapshot.paramMap.get('id');
    if (id) this.productService.get(id).take(1).subscribe(p => this.product = p);
    //if (id) this.productService.get(id).valueChanges().subscribe(p => this.product = p);
  }

  save(product){
    this.productService.create(product);
    this.router.navigate(['admin/products']);
    //console.log(product);
  }
  ngOnInit() {
  }

}

【问题讨论】:

标签: angular firebase rxjs angularfire2


【解决方案1】:

如果你的 this.productService.get(id) 方法返回一个 Observable,你必须用 pipe() 函数包围你的操作符:

this.productService.get(id).pipe(take(1)).subscribe(p => this.product = p);

【讨论】:

    猜你喜欢
    • 2021-05-15
    • 2021-10-22
    • 2021-12-01
    • 1970-01-01
    • 2019-01-21
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    相关资源
    最近更新 更多