Angular - Type Pipe 没有“?mod”属性

Aut*_*zer 12 pipe angular

我正在尝试创建一个自定义管道,该管道将返回表中数组的总和,但无论出于何种原因,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);
  }
}
Run Code Online (Sandbox Code Playgroud)

我的模块:

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 { }
Run Code Online (Sandbox Code Playgroud)

我的组件 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>
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

我不确定它是否以任何方式相关,但我的 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 {}
Run Code Online (Sandbox Code Playgroud)

ari*_*dim 48

长话短说:rm -rf .angular

我突然在一个旧的工作项目上遇到了这个问题,也适用于旧的提交。没有什么对我有用:

  • 删除node_modules并运行npm i
  • 再次运行ng s
  • 重新启动我的电脑

我有一些奇怪的线索(在不同的端口上工作,当我删除文件夹并重新克隆存储库时工作)导致我的工作解决方案:删除.angular文件夹(包含缓存文件夹)

  • 这适用于我的场景:我们使用 Angular 13 并切换到使用 pnpm 并收到 emod 错误。 (6认同)

Aut*_*zer 14

事实证明,解决方案是将 FieldSumPipe 放在我的模块的declarationsandexports中而不是imports.

import { SomeComponent} from './some.component';
import { SomeRoutingModule} from './some-routing.module';
import { FieldSumPipe } from '../../../shared/pipes/fieldsum.pipe';
// Other imports...

@NgModule({
    imports: [
        CommonModule,
        SomeRoutingModule, // other imports removed for brevity
    ],
    declarations: [
        FieldSumPipe,
        SomeComponent,
    ],
    exports: [
        FieldSumPipe
    ]
})
export class SomeModule { }
Run Code Online (Sandbox Code Playgroud)


Bja*_*sen 13

我遇到了同样奇怪的错误,缺少“emod”属性 - 只是使用服务。我忘记了服务进入模块中的 Providers 数组,而不是 Imports。我想他们可以写出更好的错误消息。

服务属于providers数组


小智 8

您必须仅在声明中添加它。

像这样:

import { SomeComponent} from './some.component';
import { SomeRoutingModule} from './some-routing.module';
import { FieldSumPipe } from '../../../shared/pipes/fieldsum.pipe';
// Other imports...

@NgModule({
    imports: [
        CommonModule,
        SomeRoutingModule
    ],
    declarations: [
        SomeComponent,
        FieldSumPipe
    ]
})
export class SomeModule { }
Run Code Online (Sandbox Code Playgroud)


Ale*_*lor 7

我遇到了类似的错误,仅通过重新启动ng serve服务器就修复了它。

(对不起,我没有回答最初的问题,但这可能对搜索错误并到达这里的其他人有用。)