【发布时间】:2019-09-30 14:28:00
【问题描述】:
我能够从 phpmyadmin 数据库中获取博客类别名称。 我要链接分类名称
categories.component.html:
<div class="right-widget categories">
<h3>Categories</h3>
<ul>
<li *ngFor="let category of categories">
{{category.name}}
</li>
</ul>
</div>
blogpost.service.ts:
export class BlogpostService {
getFeaturedBlogs() {
throw new Error("Method not implemented.");
}
ServerUrl = 'http://localhost/demo/blogger/';
errorData: {};
constructor(private http: HttpClient) { }
getBlog(id: number) {
return this.http.get<Blogpost>(this.ServerUrl + 'api/blog/' + id)
.pipe(
catchError(this.handleError)
);
}
getRecentBlogs() {
return this.http.get<Blogpost>(this.ServerUrl + 'api/recent_blogs').pipe(
catchError(this.handleError)
);
}
getCategories() {
return this.http.get<Category>(this.ServerUrl + 'api/categories').pipe(
catchError(this.handleError)
);
}
categories.component.ts
export class CategoriesComponent implements OnInit {
categories: Category;
error: {};
constructor(private blogpostService: BlogpostService) { }
ngOnInit() {
this.blogpostService.getCategories().subscribe(
(data: Category) => this.categories = data,
error => this.error = error
);
}
}
你能帮帮我吗?我无法关联类别名称?
【问题讨论】:
-
我没有看到你在任何地方定义属性
categories。 -
我认为您想将响应类型转换为 Category 数组对象。如果您已将类别定义为类,则它将不起作用。您需要创建一个具有所需属性的接口。看到这个答案 - stackoverflow.com/a/58154215/5396181
标签: angular