typescript 模块“AppModule”导入的意外值“[object Object]”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43078541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:23:31  来源:igfitidea点击:

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

angulartypescriptmodule

提问by Kaan Bursa

I want to import Pipe Module to my app,

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

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: [],
      };
   }
}

this is my pipe module

这是我的管道模块

at my app.module.ts

在我的 app.module.ts

imports: [
   ...,
    PipeModule.forRoot()

I import it like this and I get this error

我像这样导入它,但出现此错误

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

回答by Francesco Borzi

I got the same error (which is very generic), but for a totally different reason.

我遇到了同样的错误(这是非常通用的),但出于完全不同的原因。

My solution won't solve the original question, but I write it here anyway in case someone has the same problem:

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

In my unit tests, I was accidentally declaring a providerobject inside the importsarray instead of the providersone. That was causing the problem.

在我的单元测试中,我不小心providerimports数组中声明了一个对象而不是一个对象providers。那是造成问题的原因。

回答by snorkpete

Your class PipeModuleis not decorated with the @NgModuledecorator - so it's not a module. Hence it can't be imported into your AppModule- only @NgModulesare allowed to be imported.

你的类PipeModule没有用@NgModule装饰器装饰 - 所以它不是一个模块。因此它不能导入到您的AppModule- 只@NgModules允许导入。

Based on what you're doing, your first code example is just creating a standard Angular pipe so please change the name of that class to something like ValuesPipeso it's not misleading.

根据您在做什么,您的第一个代码示例只是创建一个标准的 Angular 管道,因此请将该类的名称更改为类似的名称,ValuesPipe以免误导。

At that point, you have two options: you can declare your new ValuesPipein AppModuleor you can create a whole new NgModuleto store your new ValuesPipeand then import that new NgModuleinto your AppModule.

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

Please note that in both options below, I've assumed that you've renamed the class in your first example to ValuesPipe.

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

so, Option 1:

因此,选项 1:

in app.module.ts,

app.module.ts 中

declarations: [
  .....,
  ValuesPipe,
]

Or Option 2:

选项2:

create a new class PipeModulelooking like:

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

@NgModule({
   imports: CommonModule,  (from @angular/core)
   declarations: ValuesPipe,
})
export class PipeModule { }

And then, in app.module.ts,

然后,在app.module.ts 中

imports: [
  ......,
  PipeModule
]