Angular 错误TS2339:属性';主队';不存在于类型';{}';
当我编译angular 2项目时,我面临以下错误。我对angular 2还不熟悉,似乎无法对该决议作出结论。我搜索的线程非常类似于此,但它不工作Angular 错误TS2339:属性';主队';不存在于类型';{}';,angular,Angular,当我编译angular 2项目时,我面临以下错误。我对angular 2还不熟悉,似乎无法对该决议作出结论。我搜索的线程非常类似于此,但它不工作 错误TS2339:类型“{}”上不存在属性“hometeam”。 hometeam是我返回的数据中的一个对象。他们知道为什么不把它当作目标 我正在进行的多个服务调用中出现此错误。这是一个平均堆栈应用程序。请看一看截图 代码: this.authService.gettodaysmatches().subscribe((data:any) =>
this.authService.gettodaysmatches().subscribe((data:any) => {
console.log("Data for today's matches coming up Admin panel...");
console.log(data);
this.todaysMatches = data;
//this.matchId = this.todaysMatches._id;
for(var i=0; i<this.todaysMatches.length; i++){
var hometeam = this.todaysMatches[i].hometeam.teamname;
var awayteam = this.todaysMatches[i].awayteam.teamname;
var obj = {};
obj.hometeam = hometeam;
obj.awayteam = awayteam;
obj.matchid = this.todaysMatches[i]._id;
this.todaysTeam.push(obj);
}
})
this.authService.gettodaysmatches().subscribe((数据:any)=>{
log(“今天比赛的数据将出现在管理面板…”);
控制台日志(数据);
this.todaysMatches=数据;
//this.matchId=this.todaysMatches.\u id;
对于(var i=0;i,您可以为todaysMatches
-todaysMatches创建自定义类型并强制转换它:
this.authService.gettodaysmatches().subscribe((data:TodaysMatches) => {
<...>
p、 该错误的屏幕截图显示了一系列与Typescript相关的错误,这些错误与您发布的代码片段无关您可以执行以下操作:
// Api returned Type
export interface MatchModel{
id: number;
title: string;
hometeam: string;
}
// Light Model.
export interface OtherModel {
id: Number;
title: string;
}
@Injectable()
export class ApiService {
/**
* Dummy observable to fake Api answer. Return Array of MatchModel
*/
getDummyData(): Observable<MatchModel[]> {
return Observable.create((obs) => {
obs.next([
{
id: 1,
title: "foo",
hometeam: "bar"
},
{
id: 2,
title: "ksdof",
hometeam: "koko"
},
{
id: 3,
title: "fokokoo",
hometeam: "sdfsdf"
}
]);
});
}
}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
providers: [ApiService],
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
public matchs: MatchModel[];
public others: OtherModel[];
constructor(private api: ApiService) {
this.matchs = [];
this.others = [];
}
ngOnInit() {
this.api.getDummyData().subscribe(matchs => {
this.matchs = matchs;
// We just map array of MatchModel to fit on OtherModel type.
this.others = this.matchs.map(match => {
return {
id : match.id,
title: match.title
};
});
console.log(this.matchs, this.others);
});
}
}
//Api返回的类型
导出接口匹配模型{
id:编号;
标题:字符串;
主队:弦;
}
//灯光模型。
导出接口模型{
id:编号;
标题:字符串;
}
@可注射()
出口级服务{
/**
*伪可观察到假Api答案。返回MatchModel数组
*/
getDummyData():可观察{
返回可观察。创建((obs)=>{
下一个([
{
id:1,
标题:“富”,
主队:“酒吧”
},
{
id:2,
标题:“ksdof”,
主队:“科科”
},
{
id:3,
标题:“福可古”,
主队:“sdfsdf”
}
]);
});
}
}
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
提供者:[ApiService],
样式URL:['./app.component.css']
})
导出类AppComponent实现OnInit{
公共匹配:匹配模型[];
公共其他:其他模型[];
构造函数(私有api:ApiService){
this.matchs=[];
这个。其他=[];
}
恩戈尼尼特(){
this.api.getDummyData().subscribe(matchs=>{
this.matchs=matchs;
//我们只是将MatchModel的数组映射到其他模型类型上。
this.others=this.matchs.map(match=>{
返回{
id:match.id,
标题:match.title
};
});
console.log(this.matchs,this.others);
});
}
}
尝试了第二种方法,但它不起作用……除了['prop']之外,我还需要做什么呢,所以console.log(data);
不会打印todaysMatches
对象,对吗?它是一个空对象?很好……我又发现了一个问题,我声明了var obj={};当我放置obj.teamname时,它显示错误……要么我需要初始化密钥,要么我需要使用['prop']
// Api returned Type
export interface MatchModel{
id: number;
title: string;
hometeam: string;
}
// Light Model.
export interface OtherModel {
id: Number;
title: string;
}
@Injectable()
export class ApiService {
/**
* Dummy observable to fake Api answer. Return Array of MatchModel
*/
getDummyData(): Observable<MatchModel[]> {
return Observable.create((obs) => {
obs.next([
{
id: 1,
title: "foo",
hometeam: "bar"
},
{
id: 2,
title: "ksdof",
hometeam: "koko"
},
{
id: 3,
title: "fokokoo",
hometeam: "sdfsdf"
}
]);
});
}
}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
providers: [ApiService],
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
public matchs: MatchModel[];
public others: OtherModel[];
constructor(private api: ApiService) {
this.matchs = [];
this.others = [];
}
ngOnInit() {
this.api.getDummyData().subscribe(matchs => {
this.matchs = matchs;
// We just map array of MatchModel to fit on OtherModel type.
this.others = this.matchs.map(match => {
return {
id : match.id,
title: match.title
};
});
console.log(this.matchs, this.others);
});
}
}