Angular - Type Pipe 没有 'ɵmod' 属性 [英] Angular - Type Pipe does not have 'ɵmod' property

查看:354
本文介绍了Angular - Type Pipe 没有 'ɵmod' 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义管道,该管道将返回表中数组的总和,但无论出于何种原因,Angular 都在抱怨我的管道没有emod"属性.

I am attempting to create a custom pipe that would return a sum of an array in a table, but for whatever reason, Angular is complaining about my pipe not having an 'emod' property.

我的烟斗:

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 声称 找不到管道fieldSum",但它确实成功编译了所有内容,并设法在浏览器中打开.但是,当我单击应该加载我的组件的任何内容时,控制台会显示错误.

VS Code is claiming that The pipe 'fieldSum' could not be found, but it does compile everything successfully and it manages to open in browser. However, when I click on whatever is supposed to load my component, the console displays an error.

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 中.

I am not sure if it's related in any way, but my SomeModule is lazy loaded in the 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 {}

推荐答案

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

As it turns out, the solution was to to put the FieldSumPipe in the declarations and exports of my module rather than the 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 { }

这篇关于Angular - Type Pipe 没有 'ɵmod' 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆