![](/img/trans.png)
[英]:4200//assets/config.json:1 Failed to load resource: the server responded with a status of 404 (Not Found)
[英]:4200/null Failed to load resource: the server responded with a status of 404 (Not Found)
我正在使用Angular 5.0.0和AngularFire2 5
当我在使用通过URL传递的UID从firebase加载信息的页面上时,控制台中会出现错误。 然后,该信息以表格形式显示。 这是我的CRUD的一部分,更具体地说是“更新/编辑”部分。
页面加载时在控制台中收到的错误:
:4200/null Failed to load resource: the server responded with a status of 404 (Not Found)
一切似乎都正常工作? 我不确定如何诊断此错误。 我认为自己是一个初学者,所以请以学习和理解的方式指导我。 如果还有其他需要提供的信息,请在下面告诉我。
谢谢你的时间。
产品表格。组件
import { Component, OnInit } from '@angular/core';
import { CategoryService } from '../../category.service';
import { ProductService } from '../../product.service';
import { Router } from '@angular/router';
import { 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 = {};
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);
}
}
save(product) {
this.productService.create(product);
this.router.navigate(['/admin/products']);
}
ngOnInit() {
}
}
product-form.component.html
<div class="row">
<div class="col-md-6">
<form #form="ngForm" (ngSubmit)="save(form.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">
<span class="input-group-addon">$</span>
<input #price="ngModel" 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 should be 0 or higher.</div>
</div>
</div>
<div class="form-group">
<label for="category">Category</label>
<select #category="ngModel" ngModel [(ngModel)]="product.category" name="category" id="category" class="form-control" required>
<option value=""></option>
<option *ngFor="let category of categories$ | async" [value]="category.key">{{ category.payload.val().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 [(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">Image URL is required.</div>
<div *ngIf="imageUrl.errors.url">Please enter a valid URL.</div>
</div>
</div>
<button class="btn btn-primary">Save</button>
</form>
</div>
<div class="col-md-6">
<div class="card" style="width: 20rem;">
<img class="card-img-top" [src]="product.imageUrl">
<div class="card-block">
<h4 class="card-title">{{ product.title }}</h4>
<p class="card-text">{{ product.price | currency: 'USD': symbol }}</p>
</div>
</div>
</div>
</div>
我相信问题是页面首次加载时,产品图像url最初并未从数据库加载。 所以...
在html模板的底部,向产品添加* ngIf指令。
*ngIf="product.imageUrl"
product-form.component.html
<div class="col-md-6">
<div class="card" style="width: 20rem;">
<img class="card-img-top" [src]="product.imageUrl" *ngIf="product.imageUrl">
<div class="card-block">
<h4 class="card-title">{{ product.title }}</h4>
<p class="card-text">{{ product.price | currency: 'USD': symbol }}</p>
</div>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.