Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 意外值';[对象对象]';由模块导入';AppModule';_Angular_Typescript_Module - Fatal编程技术网

Angular 意外值';[对象对象]';由模块导入';AppModule';

Angular 意外值';[对象对象]';由模块导入';AppModule';,angular,typescript,module,Angular,Typescript,Module,我想将管道模块导入我的应用程序 import { NgModule } from '@angular/core'; import { PipeTransform, Pipe } from '@angular/core'; @Pipe({ name: 'values', pure: false }) export class PipeModule implements PipeTransform { transform(value: any, args: any[] = null): an

我想将管道模块导入我的应用程序

import { NgModule } from '@angular/core';
import { PipeTransform, Pipe } from '@angular/core';

@Pipe({ name: 'values',  pure: false })
export class PipeModule implements PipeTransform {
  transform(value: any, args: any[] = null): any {
    return Object.keys(value).map(key => value[key]);
  }

  static forRoot() {
      return {
          ngModule: PipeModule,
          providers: [],
      };
   }
}
这是我的管道模块

在我 应用程序模块.ts

imports: [
   ...,
    PipeModule.forRoot()
我像这样导入它,我得到了这个错误

Unexpected value '[object Object]' imported by the module 'AppModule

您的类
PipeModule
没有使用
@NgModule
装饰器装饰-因此它不是一个模块。因此,它无法导入到您的
AppModule
-只允许导入
@NgModules

根据您所做的工作,您的第一个代码示例只是创建一个标准的角度管道,因此请将该类的名称更改为类似于
ValuesPipe
的名称,以避免误导

此时,您有两个选项:您可以在
AppModule
中声明新的
ValuesPipe
,或者您可以创建一个全新的
NgModule
来存储新的
ValuesPipe
,然后将新的
NgModule
导入
AppModule

请注意,在下面的两个选项中,我假设您已将第一个示例中的类重命名为
ValuesPipe

因此,选项1:

在app.module.ts中

declarations: [
  .....,
  ValuesPipe,
]
imports: [
  ......,
  PipeModule
]
选项2:

创建一个新类PipeModule,如下所示:

@NgModule({
   imports: CommonModule,  (from @angular/core)
   declarations: ValuesPipe,
})
export class PipeModule { }
然后,在app.module.ts中

declarations: [
  .....,
  ValuesPipe,
]
imports: [
  ......,
  PipeModule
]
我得到了同样的错误(这是非常普遍的),但原因完全不同

我的解决方案不会解决原来的问题,但我还是将其写在这里,以防有人遇到同样的问题:


在单元测试中,我意外地在
imports
数组中声明了一个
provider
对象,而不是
providers
对象。这就是问题的原因。

谢谢您给出的明确答案!我使用了声明部分,我发现它更容易实现