[英]Typescript typed array not displaying data
I am using Angular 7 and Typescript 3 to create a pre-populate array of ingredients in a service that will be used in a component but when I print the array out to the console, I am getting an array of empty objects. 我正在使用Angular 7和Typescript 3在将要在组件中使用的服务中创建填充前的成分数组,但是当我将其打印到控制台时,却得到了一个空对象数组。
If I create the typed array using object literals it will work, but if I create the array using the new operator, the array, will not contain any data. 如果我使用对象文字创建类型化的数组,它将起作用,但是如果我使用new运算符创建该数组,则该数组将不包含任何数据。
EDITED: Added snippet of ingredient class 编辑:添加了成分类别的代码段
export class Ingredient {
constructor(name?: string, amount?: number, measurement?: string) {}
}
This contains data: 其中包含数据:
export class ShoppingListService {
private ingredients: Ingredient[] = [{
name: 'shrimp',
amount: 1,
measurement: 'cup'
},
{
name: 'bib lettuce',
amount: 1,
measurement: 'bunch'
}];
constructor() {
console.log('ingredients', this.ingredients);
}
console output: 控制台输出:
ingredients [{name: "shrimp", amount: 1, measurement: "cup"},
{name: "bib lettuce", amount: 1, measurement: "bunch"}
]
This this doesn't contain data 这个不包含数据
export class ShoppingListService {
private ingredients = [
new Ingredient('shrimp', 1, 'cup'),
new Ingredient('bib lettuce', 1, 'bunch')
];
constructor() {
console.log('ingredients', this.ingredients);
}
}
console output: 控制台输出:
ingredients [Ingredient{}, Ingredient{}]
I have also tried using the following syntax, but I get the same output as the example above: 我也尝试使用以下语法,但是得到的输出与上面的示例相同:
private ingredients: Ingredient[] = [
new Ingredient('shrimp', 1, 'cup'),
new Ingredient('bib lettuce', 1, 'bunch')
];
is there some typescript or angular logic that I am missing here? 我在这里缺少一些打字稿或角度逻辑吗? The example above is used in the Angular docs here: Angular: Using the Hero class
上面的示例在此处的Angular文档中使用 : Angular:使用Hero类
So i think you need to these changes in your Ingredient class 所以我认为您需要在成分班级中进行这些更改
export class Ingredient {
constructor(data?: any) {
this.name = data.name;
this.amount = data.amount;
this.measurement = data.measurement;
}
public name: string = null;
public amount: number = null;
public measurement: string = null;
}
And inside your component for setting a data 在您的组件内部用于设置数据
private ingredients = [
new Ingredient({
name: 'bib lettuce',
amount: 1,
measurement: 'bunch'
})
];
console.log(ingredients);
I hope it helps you out 希望对您有帮助
**Working code for both scenario**
//ingredient.ts
export class Ingredient{
constructor(public name: string, public id: number, public measurment: string){
}
}
//ingredient.component.ts
import {Component} from '@angular/core';
import { Ingredient } from './ingredient ';
@Component({
selector: 'app-ingredient',
template: 'Ingredient'
})
export class IngredientComponent{
private ingredients: Ingredient[] = [{
name: 'shrimp',
id: 1,
measurment: 'cup'
},
{
name: 'bib lettuce',
id: 1,
measurment: 'bunch'
}];
/*
ingredients = [
new Ingredient('shrimp', 1, 'cup'),
new Ingredient('bib lettuce', 1, 'bunch')
];
*/
constructor(){
console.log(this.ingredients);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.