gpt4 book ai didi

angular - 属性 'valueChanges' 在类型 'Observable' 上不存在

转载 作者:行者123 更新时间:2023-12-04 10:40:32 26 4
gpt4 key购买 nike

我的组件文件

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

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

categories$;

product: any = {};

constructor(categoryservice: CategoriesService , private productservice: ProductService , private router: Router,
private route: ActivatedRoute) {
this.categories$ = categoryservice.getcategories();

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

save(product) {
this.productservice.create(product);
this.router.navigate(['/admin/products']);
}

ngOnInit() {
}

}

我的 HTML 文件
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title >Title</title>
</head>

</html>

<div class="row">
<div class="col-md-6">

<form #f="ngForm" (ngSubmit)="save(f.value)">

<div class="form-group">
<label for="title">Title</label>
<input #title="ngModel" [(ngModel)]="product.title" name="title" id="title" type="text" class="form-control" required>
<div class="alert alert-danger" *ngIf="title.touched && title.invalid">
Title is required
</div>
</div>

<div class="form-group">
<label for="price">Price</label>
<div class="input-group-prepend">
<span class="input-group-text">$</span>
<input #price="ngModel" [(ngModel)]="product.price" name="price" id="price" type="number" class="form-control" required [min]="0">
</div>
<div class="alert alert-danger" *ngIf="price.touched && price.invalid">
<div *ngIf="price.errors.required">Price is required</div>
<div *ngIf="price.errors.min">Price must be 0 or higher than zero</div>
</div>
</div>

<div class="form-group">
<label for="category">Cateogary</label>
<select #category="ngModel" [(ngModel)]="product.category" name="category" id="category" class="form-control" required>
<option value=""></option>
<option *ngFor="let c of categories$ | async" [value]="c.$key">

{{ c.name }}

</option>
</select>
<div class="alert alert-danger" *ngIf="category.touched && category.invalid">
Category is required
</div>
</div>

<div class="form-group">
<label for="imageurl">Image Url</label>
<input #imageUrl="ngModel" [(ngModel)]="product.imageUrl" name="imageUrl" id="imageurl" type="text" class="form-control" required url>
<div class="alert alert-danger" *ngIf="imageUrl.touched && imageUrl.invalid">
<div *ngIf="imageUrl.errors.required">ImageUrl is required</div>
<div *ngIf="imageUrl.errors.url">ImageUrl is required</div>

</div>
</div>

<button class="btn btn-primary">Save</button>

</form>
</div>

<div class="col-md-6">
<div class="card" style="width: 18rem;">
<img [src]="product.imageUrl" class="card-img-top" alt="..." *ngIf="product.imageUrl">
<div class="card-body">
<h5 class="card-title"> {{product.title}} </h5>
<p class="card-text"> {{product.price | currency:'USD':true}} </p>

</div>
</div>
</div>
</div>

这是我的 我的产品服务:文件
import { Product } from './admin/product-forms/product';
import { AngularFireDatabase } from '@angular/fire/database';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';


@Injectable({
providedIn: 'root'
})
export class ProductService {

constructor(private db: AngularFireDatabase) { }

create(product) {
return this.db.list('/products').push(product);
}

getAll() {
return this.db.list('/products');
}

get(productId): Observable<any> {
return this.db.object('/products' + productId).valueChanges();
}
}

我在线收到此错误:“
    if (id) { 
this.productservice
.get(id)
.valueChanges()
.pipe(take(1))
.subscribe(p => this.product = p);

当我删除 valueChanges() 时。我面临另一个错误:
当我在部署项目后单击浏览器中的任何输入字段时。然后我可以上面的错误...

"TypeError: Cannot read property 'title' of undefined".



有人可以帮我解决这个问题。
谢谢你!

最佳答案

您的产品服务的get(id)方法返回一个 Observable。您可能看到了 valueChanges()在 FormControl 或其他东西上,但是在使用 Observables 时,您几乎总是从 pipe() 开始或 subscribe() .

Learn RxJs是一个有用的资源。 Angular 文档也有很多很好的例子。

关于angular - 属性 'valueChanges' 在类型 'Observable<any>' 上不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59954220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com