gpt4 book ai didi

typescript - 尝试访问类型对象中缺少角度 6 属性 id 中的对象数据

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:12 26 4
gpt4 key购买 nike

我创建了一个如下所示的界面:-

export interface Recipe {
id: number;
likes: number[];
}

我的组件看起来像这样:-

import { Component, OnInit } from '@angular/core';
import {RecipeService } from '../../recipe.service';
import {Observable } from 'rxjs';
import {Recipe} from '../../recipe.interface';


@Component({
selector: 'app-recent-blog-recipes',
templateUrl: './recent-blog-recipes.component.html',
styleUrls: ['./recent-blog-recipes.component.scss'],

})
export class RecentBlogRecipesComponent implements OnInit {

recipes$: Object;
likes$ : Recipe[];

constructor(private data: RecipeService ) { }

ngOnInit() {
this.data.getUserId().subscribe(
data => {
this.likes$.push(data);
console.log(this.likes$);
},
error => console.log(error)
)
}
}

当我编译它时,我得到这个错误:

ERROR in src/app/welcome/recent-blog-recipes/recent-blog-recipes.component.ts(52,26): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'Recipe'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'id' is missing in type 'Object'.

这怎么可能,而且我在界面中有 ID?在我尝试将其推送到“likes$[]”数组之前,我从该对象记录的数据也如下所示:-

{id: 1 , 喜欢:[1,2,12]}

知道哪里出了问题吗?

最佳答案

因为 likes$Recipe[] , 你需要确保 getUserId 的返回类型可观察到 any或任何其他可分配给 Recipe 的类型.

public getUserId(): Observable<Recipe>{
return this.http.get<Recipe>('...');
}

关于typescript - 尝试访问类型对象中缺少角度 6 属性 id 中的对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50885003/

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