تايب سكريبت - النوع 'string' لا يمكن تعيينه إلى النوع 'number' عند استخدام ربط الخاصية في قالب Angular
الحصول على الأخطاء التالية:
Error: src/app/pages/journal-list/journal-list.component.html:27:23 - error TS2322: Type 'string' is not assignable to type 'number'.
27 [title]="entry.title" [date]="entry.date"
~~~~~~~~~~~~~~~~~~~
src/app/pages/journal-list/journal-list.component.ts:7:16
7 templateUrl: './journal-list.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component JournalListComponent.
src/app/pages/journal-list/journal-list.component.html:26:1 - error TS2322: Type 'number' is not assignable to type 'string | any[] | null | undefined'.
26 [routerLink] ="i"
~~~~~~~~~~~~~~~~~
src/app/pages/journal-list/journal-list.component.ts:7:16
7 templateUrl: './journal-list.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component JournalListComponent.
من الواضح أن السبب هو أنني أستخدم ربط الخاصية [date]="entry.date". لقد أعلنت عن الخاصية date كعدد. لماذا يحدث هذا؟
مقتطف كود للسياق:
<div class="entries-list">
<app-journal-entry *ngFor="let entry of entries; index as i"
[routerLink] ="i"
[title]="entry.title" [date]="entry.date"
[body]="entry.body"></app-journal-entry>
</div>
الإجابة رقم 1
يحدث الخطأ عندما يكون strictTemplates مفعلًا، أعتقد أنه خطأ برمجي.
يمكنك استخدام "$any(*)"
لذلك بدلاً من استخدام [title]="entry.title"
يجب عليك استخدام [title]=$any(entry.title)
لمزيد من المعلومات، انظر الرابط https://github.com/angular-split/angular-split/issues/220