gpt4 book ai didi

javascript - 核心.js :6406 ERROR TypeError: Cannot read property 'id' of undefined

转载 作者:行者123 更新时间:2023-12-04 15:31:39 24 4
gpt4 key购买 nike

我有一个 Angular 8 应用程序。还有一个创建功能。但我无法创建项目,因为 id 为空。
当然,我为此搜索了谷歌。但我认为这是一个具体案例。
所以这是触发函数的模板:

<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)
idnull .
但我必须改变什么?
谢谢
更新:
这是添加新项目的链接: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
}
},
然后我可以创建一个新项目。但这不是我想要的,因为新项目将被加载到新 View 中。
所以这就是为什么我有这个:
 {
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 为空:
 this.dossier.id = params.dossierId;
我的路线是这样的:
  {
path: ':dossierId',
component: ViewComponent, children: [
{ path: 'item/new/:dossierItemType', component: ItemComponent}
],
resolve: {
dossier: DossierResolver,
dossierItems: DossierItemsResolver
}
},

enter image description here
enter image description here
所以这被调用了两次:
 this.route.params.subscribe((params: Params) => {
this.dossier.id = params.dossierId;
});
不明白为什么

最佳答案

可以试试paramsInheritanceStrategy ?

@NgModule({
imports: [RouterModule.forRoot(routes,{
paramsInheritanceStrategy: 'always'
})],
exports: [RouterModule]
})
export class AppRoutingModule { }

您还需要捕获父路由参数

关于javascript - 核心.js :6406 ERROR TypeError: Cannot read property 'id' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61134693/

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