【发布时间】:2017-01-20 13:55:41
【问题描述】:
我是 Angular2 世界的新手。为了学习 Angular,我遵循了不同的教程,现在我尝试建立一个商店以了解更多信息。但是我马上就卡住了。
但我没有成功。我收到不同的错误,例如。
TypeError:无法读取未定义和错误的属性“描述” TS2322:类型“{ ... }”不可分配给类型“Product[]”。类型 ...
这是我目前得到的:
产品类型属性模型
export class ProductTypeAttribute {
id: number;
name: string;
content: string;
}
产品类型模型
import { ProductTypeAttribute } from './product-type-attribute.model';
export class ProductType {
id: number;
name: string;
description: string;
attributeTypes: ProductTypeAttribute[];
}
产品型号
import { ProductType} from './product-type.model';
export class Product {
id: number;
name: string;
description: string;
image: string;
price: number;
type: ProductType;
}
模拟产品
import { Product } from './product.model';
import { ProductType } from './product-type.model';
import { ProductTypeAttribute } from './product-type-attribute.model';
export const productTypeAttributes: ProductTypeAttribute[] = [
{
id: 1,
name: 'Kleur',
content: 'test'
},
{
id: 2,
name: 'test',
content: 'test'
},
{
id: 3,
name: 'test',
content: 'test'
},
{
id: 4,
name: 'test',
content: 'test'
}
];
export const productTypeAttributes2: ProductTypeAttribute[] = [
{
id: 5,
name: 'Kleur',
content: 'test'
},
{
id: 6,
name: 'test',
content: 'test'
},
{
id: 7,
name: 'test',
content: 'test'
},
{
id: 8,
name: 'test',
content: 'test'
}
];
export const productType: ProductType =
{
id: 1,
name: 'Type 1',
description: 'Description of type 1',
attributeTypes: productTypeAttributes
};
export const productType2: ProductType =
{
id: 2,
name: 'Type 2',
description: 'Description of type 2',
attributeTypes: productTypeAttributes2
};
export const products: Product[] = [
{
id: 1,
name: 'Product 1',
description: 'Description of product 1',
image: 'https://www.google.nl/imgres?imgurl=https%3A%2F%2Fangular.io%2Fresources%2Fimages%2Flogos%2Fangular2%2Fangular.svg&imgrefurl=https%3A%2F%2Fangular.io%2F&docid=bJoyJcb-C12SHM&tbnid=G_BYSyR7DGpqqM%3A&vet=1&w=800&h=800&bih=1060&biw=1060&q=angular2&ved=0ahUKEwjdrq6it87RAhUEExoKHdXlAYIQMwgcKAAwAA&iact=mrc&uact=8',
price: 15.15,
type: productType
},
{
id: 2,
name: 'Product 2',
description: 'Description of product 2',
image: 'https://www.google.nl/imgres?imgurl=https%3A%2F%2Fangular.io%2Fresources%2Fimages%2Flogos%2Fangular2%2Fangular.svg&imgrefurl=https%3A%2F%2Fangular.io%2F&docid=bJoyJcb-C12SHM&tbnid=G_BYSyR7DGpqqM%3A&vet=1&w=800&h=800&bih=1060&biw=1060&q=angular2&ved=0ahUKEwjdrq6it87RAhUEExoKHdXlAYIQMwgcKAAwAA&iact=mrc&uact=8',
price: 15.15,
type: productType
},
{
id: 3,
name: 'Product 3',
description: 'Description of product 3',
image: 'https://www.google.nl/imgres?imgurl=https%3A%2F%2Fangular.io%2Fresources%2Fimages%2Flogos%2Fangular2%2Fangular.svg&imgrefurl=https%3A%2F%2Fangular.io%2F&docid=bJoyJcb-C12SHM&tbnid=G_BYSyR7DGpqqM%3A&vet=1&w=800&h=800&bih=1060&biw=1060&q=angular2&ved=0ahUKEwjdrq6it87RAhUEExoKHdXlAYIQMwgcKAAwAA&iact=mrc&uact=8',
price: 15.15,
type: productType2
}
];
产品-服务
import { Injectable } from '@angular/core';
import { Product } from './../shared/product.model';
import { products } from './../shared/mock-products';
@Injectable()
export class ProductService {
getProducts(): Promise<Product[]> {
return Promise.resolve(products);
}
getProduct(id: number): Promise<Product> {
return this.getProducts().then(products => products.find(product => product.id === id));
}
}
产品组件
import { Component, OnInit } from '@angular/core';
import { Product } from './../shared/product.model';
import { ProductService } from './../product/product.service';
@Component({
selector: 'product',
template: require('./product.component.html'),
providers: [ProductService]
})
export class ProductComponent implements OnInit {
product: Product;
constructor(private productService: ProductService) { }
getProduct(): void {
this.productService.getProduct(1).then(product => this.product = product);
}
ngOnInit(): void {
this.getProduct();
}
}
产品 HTML
<div class="product">
<div class="header">
<h1>{{product?.name}}</h1>
<h4></h4>
</div>
<figure>
<img src="{{product?.image}}">
</figure>
<section>
<p>{{product?.description}}</p>
<details>
<summary>Product Features</summary>
<ul>
<li *ngFor="let productAttribute of product?.type?.attributeTypes">
{{productAttribute?.name}}
</li>
</ul>
</details>
<button>Buy Now</button>
</section>
我希望我能很好地解释我的问题,并且你们可以在这里帮助我。
*更新** 好吧,现在事情变得疯狂了。 当我这样做时
export const products: ({ id: number;description: string;image: string;price: number;type: Object } |
{ id: number;name: string;description: string;image: string;price: number;type: Object })[] = [
{
id: 1,
description: "Description of product 1",
image: "https://www.google.nl/imgres?imgurl=https%3A%2F%2Fangular.io%2Fresources%2Fimages%2Flogos%2Fangular2%2Fangular.svg&imgrefurl=https%3A%2F%2Fangular.io%2F&docid=bJoyJcb-C12SHM&tbnid=G_BYSyR7DGpqqM%3A&vet=1&w=800&h=800&bih=1060&biw=1060&q=angular2&ved=0ahUKEwjdrq6it87RAhUEExoKHdXlAYIQMwgcKAAwAA&iact=mrc&uact=8",
price: 15.15,
type: productType
},
{
id: 2,
name: "Product 2",
description: "Description of product 2",
image: "https://www.google.nl/imgres?imgurl=https%3A%2F%2Fangular.io%2Fresources%2Fimages%2Flogos%2Fangular2%2Fangular.svg&imgrefurl=https%3A%2F%2Fangular.io%2F&docid=bJoyJcb-C12SHM&tbnid=G_BYSyR7DGpqqM%3A&vet=1&w=800&h=800&bih=1060&biw=1060&q=angular2&ved=0ahUKEwjdrq6it87RAhUEExoKHdXlAYIQMwgcKAAwAA&iact=mrc&uact=8",
price: 15.15,
type: productType
},
{
id: 3,
name: "Product 3",
description: "Description of product 3",
image: "https://www.google.nl/imgres?imgurl=https%3A%2F%2Fangular.io%2Fresources%2Fimages%2Flogos%2Fangular2%2Fangular.svg&imgrefurl=https%3A%2F%2Fangular.io%2F&docid=bJoyJcb-C12SHM&tbnid=G_BYSyR7DGpqqM%3A&vet=1&w=800&h=800&bih=1060&biw=1060&q=angular2&ved=0ahUKEwjdrq6it87RAhUEExoKHdXlAYIQMwgcKAAwAA&iact=mrc&uact=8",
price: 15.15,
type: productType2
}
它没有给出错误,但不显示名称。
【问题讨论】:
-
不确定我是否理解您的意思。所以我必须在我的模型中使用构造函数或将它们更改为接口?还是应该在我的模拟中使用构造函数?
标签: javascript angular typescript