テーブル内の配列の合計を返すカスタム パイプを作成しようとしていますが、何らかの理由で、Angular はパイプに 'emod' プロパティがないとエラーを出します。
私のパイプ:
import { Injectable, Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'fieldSum',
pure: false
})
@Injectable()
export class FieldSumPipe implements PipeTransform {
transform(items: any[], attr: string): number {
return items.reduce((a, b) => a + b[attr], 0);
}
}
私のモジュール:
import { SomeComponent} from './some.component';
import { SomeRoutingModule} from './some-routing.module';
import { FieldSumPipe } from '../../../shared/pipes/fieldsum.pipe';
// Other imports...
@NgModule({
imports: [
CommonModule,
SomeRoutingModule,
FieldSumPipe, // other imports removed for brevity
],
declarations: [
SomeComponent,
],
exports: [
FieldSumPipe
]
})
export class SomeModule { }
私のコンポーネント HTML:
<table class="table table-striped table-hover table-responsive-lg" [mfData]="tableData" #mf="mfDataTable" [mfRowsOnPage]="10">
<thead>
<tr>
<th>...</th>
<th>...</th>
<th>...</th>
<th>Profit</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of mf.data">
<td>{{ item.Id }}</td>
<td>{{ item.Name }}</td>
<td>{{ item.Something }}</td>
<td>{{ item.Profit }}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td>{{ mf.data | fieldSum:'Profit' }}</td>
</tr>
</tfoot>
</table>
VS Code は と主張していますThe pipe 'fieldSum' could not be found
が、すべて正常にコンパイルされ、ブラウザーで開くことができます。ただし、コンポーネントをロードするはずのものをクリックすると、コンソールにエラーが表示されます。
core.js:4442 ERROR Error: Uncaught (in promise): Error: Type FieldSumPipe does not have 'ɵmod' property.
Error: Type FieldSumPipe does not have 'ɵmod' property.
at getNgModuleDef (core.js:1855)
at recurse (core.js:24235)
at recurse (core.js:24246)
at registerNgModuleType (core.js:24231)
at new NgModuleFactory$1 (core.js:24345)
at Compiler_compileModuleSync__POST_R3__ (core.js:27135)
at Compiler_compileModuleAsync__POST_R3__ [as compileModuleAsync] (core.js:27140)
at MergeMapSubscriber.project (router.js:3506)
at MergeMapSubscriber._tryNext (mergeMap.js:44)
at MergeMapSubscriber._next (mergeMap.js:34)
at resolvePromise (zone-evergreen.js:798)
at resolvePromise (zone-evergreen.js:750)
at zone-evergreen.js:860
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:27533)
at ZoneDelegate.invokeTask (zone-evergreen.js:398)
at Zone.runTask (zone-evergreen.js:167)
at drainMicroTaskQueue (zone-evergreen.js:569)
何らかの関連があるかどうかはわかりませんが、SomeModule は AppRoutingModule で遅延ロードされます。
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{
path: '',
data: {
title: 'Whatever',
},
children: [
{
path: 'some',
loadChildren: () =>
import('./some/some.module').then((m) => m.SomeModule),
}
]
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
ベストアンサー1
要約:rm -rf .angular
古くて動作中のプロジェクトで、古いコミットでも突然この問題が発生しました。何も機能しませんでした:
- node_modulesを削除して実行
npm i
- 再実行
ng s
- コンピュータを再起動する
いくつかの奇妙な手がかり(別のポートで動作し、フォルダーを削除してリポジトリを再クローンすると動作した)があり、それが私の実用的な解決策につながりました:.angular
フォルダー(キャッシュフォルダーを含む)を削除します