【发布时间】:2020-10-23 09:51:44
【问题描述】:
我要做什么
我正在尝试创建一个页面,其中有一堆垫子扩展面板,每个面板都有自己的内容。内容是硬编码的,但是我不想让每个页面都在它自己的组件上,我想尝试制作一个可以重用并与静态服务交互的组件,以便在需要添加更多内容时可扩展,或者如果我是将来将其连接到数据库。
我的尝试
我的尝试(变体 1)
<article *ngIf="code$ | async as title">
<h1>{{title.title}}</h1>
<div *ngFor="section of title.sections | async">
<mat-expansion-panel>
<mat-expansion-panel-header>
<mat-panel-title>
{{sTitle}}
</mat-panel-title>
<mat-panel-description>
{{sDesc}}
</mat-panel-description>
</mat-expansion-panel-header>
<div>
{{sContent}}
</div>
</mat-expansion-panel>
</div>
</article>
我的尝试(变体 2)
<article *ngIf="code$ | async as title">
<h1>{{title.title}}</h1>
<div *ngFor="section of title.sections | async as section">
<mat-expansion-panel>
<mat-expansion-panel-header>
<mat-panel-title>
{{sTitle}}
</mat-panel-title>
<mat-panel-description>
{{sDesc}}
</mat-panel-description>
</mat-expansion-panel-header>
<div>
{{sContent}}
</div>
</mat-expansion-panel>
</div>
</article>
我的尝试(变体 3)
<article *ngIf="code$ | async as title">
<h1>{{title.title}}</h1>
<div *ngFor="section of title.sections | async">
<mat-expansion-panel>
<mat-expansion-panel-header>
<mat-panel-title>
{{this.sTitle}}
</mat-panel-title>
<mat-panel-description>
{{this.sDesc}}
</mat-panel-description>
</mat-expansion-panel-header>
<div>
{{this.sContent}}
</div>
</mat-expansion-panel>
</div>
</article>
我的尝试(变体 4)
<article *ngIf="code$ | async as title">
<h1>{{title.title}}</h1>
<div *ngFor="section of title.sections | async">
<mat-expansion-panel>
<mat-expansion-panel-header>
<mat-panel-title>
{{this.section.sTitle}}
</mat-panel-title>
<mat-panel-description>
{{this.section.sDesc}}
</mat-panel-description>
</mat-expansion-panel-header>
<div>
{{this.section.sContent}}
</div>
</mat-expansion-panel>
</div>
</article>
其他代码
Component.ts 的片段(一切都在顶部很好地导入)
code$: any;
constructor(
private pcts: StaticService,
private route: ActivatedRoute
){}
ngOnInit(){
this.code$ = this.route.paramMap.pipe(
map(params => {
const title = params.get('slug');
return this.pcts.pctSearch(title);
})
)
}
服务.ts
export interface tSect {
sTitle: string;
sDesc: string;
sContent: string;
}
export interface pct {
id: string;
title: string;
sections: tSect[];
}
...
{
id: "...",
title: "...",
sections: [
{sTitle: 'Test', sDesc: 'Test', sContent: 'test'},
{sTitle: 'Tes2t', sDesc: 'Test2', sContent: 'test2'},
],
},
...
pctSearch(slug: string) {
var __FOUND = -1;
for(var t = 0; t < this.pctarr.length; t++) {
if(this.pctarr[t].id == slug) {
__FOUND = t;
return this.pctarr[__FOUND];
}
}
}
会发生什么?
我收到一条错误消息,指出组件上不存在该属性(sContent、sTitle 或 sDesc)。
预期结果
该部分应显示 mat-expansion-panel(s) 列表,其中包含服务中数组中的部分。
【问题讨论】:
标签: angular service angular-material angular9