我有一个Angular 8应用程序。并具有创建功能。但是我无法创建一个项目,因为id为null。
我当然用谷歌搜索了。但是我认为这是一个具体情况。
因此,这是将触发功能的模板:
<button *ngIf="isNew" mat-raised-button color="primary" [appSubmitIfValid]="editItemForm" (valid)="save()" i18n>Create</button>
这是实际功能:
this.dossierService.newDossierItem(this.dossier.id, dossierItemDto)
.subscribe(item => {
this.item = item;
this.dossierItems.unshift(item);
this.sortDossierItems();
this.isNew = false;
form.enable();
form.markAsPristine();
this.errorProcessor.openSuccessSnackBar($localize`Item is saved`);
}, error => this.handleError(error));
这是我的router.ts:
{
path: ':dossierId',
component: ViewComponent, children: [
{ path: 'item/new/:dossierItemType', component: ItemComponent}
],
resolve: {
dossier: DossierResolver,
dossierItems: DossierItemsResolver
}
},
所以我在这条线上出现错误:
this.dossierService.newDossierItem(this.dossier.id, dossierItemDto)
id
是null
。
但是我必须改变什么?
谢谢
这是添加新项目的链接:view.component.html:
<ng-template mat-tab-label #interviews>
<mat-icon class="interviews">speaker_notes</mat-icon>
<span i18n>Interview reports</span>{{ dossierItemsCountString(itemTypes.Interview) }}
<a [routerLink]="['../' , dossier.id , 'item' , 'new', itemTypes.Interview]">
<mat-icon class="add_box">add</mat-icon>
</a>
</ng-template>
问题出在路由上。因为如果我这样做:
{
path: ':dossierId/item/new/:dossierItemType', component: ItemComponent,
resolve: {
dossier: DossierResolver,
dossierItems: DossierItemsResolver
}
},
然后我可以创建一个新项目。但这不是我想要的,因为新项目随后将被加载到新视图中。
这就是为什么我有这个:
{
path: ':dossierId',
component: ViewComponent, children: [
{ path: 'item/new/:dossierItemType', component: ItemComponent}
],
resolve: {
dossier: DossierResolver,
dossierItems: DossierItemsResolver
}
},
但是档案每次都是空的。
这是完整的item.component.ts代码:
export class ItemComponent implements OnInit {
itemTypes = DossierItemTypeDto;
formBuilder = new FormBuilder();
isNew = false;
editItemForm: FormGroup;
dossierItemId: string;
item: DossierItemDto;
dossierItems: DossierItemDto[];
dossier: DossierDto;
globalErrors: ValidationErrors;
constructor(private dossierService: DossierService, private route: ActivatedRoute, private router: Router,
private errorProcessor: ErrorProcessor) {
this.dossier = this.route.snapshot.data.dossier;
this.dossierItemId = this.route.snapshot.params.dossierItemId;
this.isNew = this.dossierItemId === undefined;
this.dossierItems = this.route.snapshot.data.dossierItems;
if (this.isNew) {
this.item = {
title: '',
itemType: this.route.snapshot.params.dossierItemType,
date: moment().format('Y-MM-DD'),
createdAt: moment().format('Y-MM-DD'),
body: '' };
} else {
this.item = this.dossierItems.find(i => i.id === this.dossierItemId);
}
}
ngOnInit(): void {
this.editItemForm = this.formBuilder.group({
title: this.formBuilder.control(this.item.title, [Validators.required]),
itemType: this.formBuilder.control(this.item.itemType, [Validators.required]),
date: this.formBuilder.control(moment(this.item.date, 'Y-MM-DD'), [Validators.required]),
body: this.formBuilder.control(this.item.body, [Validators.required, Validators.maxLength(2097152)])
});
}
save(): void {
const form = this.editItemForm;
const dossierItemDto: DossierItemPostDto = {
title: form.controls.title.value,
itemType: form.controls.itemType.value,
date: (form.controls.date.value as moment.Moment).format('Y-MM-DD'),
body: form.controls.body.value
};
form.disable();
if (!this.isNew) {
this.dossierService.updateDossierItemById(this.dossier.id, this.item.id, dossierItemDto)
.subscribe(item => {
this.item = item;
this.sortDossierItems();
form.enable();
form.markAsPristine();
this.errorProcessor.openSuccessSnackBar($localize`Item is saved`);
}, error => this.handleError(error));
} else {
this.dossierService.newDossierItem(this.dossier.id, dossierItemDto)
.subscribe(item => {
this.item = item;
this.dossierItems.unshift(item);
this.sortDossierItems();
this.isNew = false;
form.enable();
form.markAsPristine();
this.errorProcessor.openSuccessSnackBar($localize`Item is saved`);
}, error => this.handleError(error));
}
}
}
但是,如果我尝试这样做:
{
path: ':dossierId',
component: ViewComponent, children: [
{ path: 'item/new/:dossierItemType', component: ItemComponent}
],
resolve: {
dossier: DossierResolver,
dossierItems: DossierItemsResolver
}
},
哦,这很奇怪,因为在此:
ngOnInit(): void {
this.route.params.subscribe((params: Params) => {
this.dossier.id = params.dossierId;
});
在调试器中。我看到是第一次设置ID:
"06637e72-8915-4735-9400-4ef7705194ea"
但是如果我执行f8键,则id为null:
this.dossier.id = params.dossierId;
和我的路线是这样的:
{
path: ':dossierId',
component: ViewComponent, children: [
{ path: 'item/new/:dossierItemType', component: ItemComponent}
],
resolve: {
dossier: DossierResolver,
dossierItems: DossierItemsResolver
}
},
因此,这被称为两次:
this.route.params.subscribe((params: Params) => {
this.dossier.id = params.dossierId;
});
不明白为什么
答案 0 :(得分:1)
您可以尝试paramsInheritanceStrategy
吗?
@NgModule({
imports: [RouterModule.forRoot(routes,{
paramsInheritanceStrategy: 'always'
})],
exports: [RouterModule]
})
export class AppRoutingModule { }
您还需要捕获父路径参数
答案 1 :(得分:0)
以下代码对我有用:
在您的Angular项目的tsconfig.json中
//========================
// 0 0 0
// 0 0 0
// 0 0 0
// 0 0 0
// 0 0 0
// ========================
// Ran. Number: 0 1 2 3 4 5 6 7 8 9
// Frequency(Counts): 001A148D